diff --git a/README.md b/README.md index 974f043..a25d887 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,9 @@ Basic configuration is based on [RequestInit](https://developer.mozilla.org/en-U mode: "cors", redirect: "follow", referrer: "client", + onStart: () => null, + onFinish: () => null, + onError: () => null, } ``` diff --git a/package-lock.json b/package-lock.json index 72d0281..44d7946 100644 --- a/package-lock.json +++ b/package-lock.json @@ -233,7 +233,8 @@ "@types/node": { "version": "10.12.24", "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.24.tgz", - "integrity": "sha512-GWWbvt+z9G5otRBW8rssOFgRY87J9N/qbhqfjMZ+gUuL6zoL+Hm6gP/8qQBG4jjimqdaNLCehcVapZ/Fs2WjCQ==" + "integrity": "sha512-GWWbvt+z9G5otRBW8rssOFgRY87J9N/qbhqfjMZ+gUuL6zoL+Hm6gP/8qQBG4jjimqdaNLCehcVapZ/Fs2WjCQ==", + "dev": true }, "abab": { "version": "2.0.0", @@ -1857,12 +1858,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1877,17 +1880,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2004,7 +2010,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2016,6 +2023,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2030,6 +2038,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2037,12 +2046,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -2061,6 +2072,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2141,7 +2153,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2153,6 +2166,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2274,6 +2288,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", diff --git a/package.json b/package.json index 2ef76a3..6d45ca5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "odata", "description": "o.js is a isomorphic Odata Javascript library to simplify the request of data. The main goal is to build a standalone, lightweight and easy to understand Odata lib.", - "version": "1.0.0", + "version": "1.0.1", "main": "dist/cjs/o.js", "browser": "dist/umd/o.js", "module": "dist/es2015/o.js", diff --git a/src/OHandler.ts b/src/OHandler.ts index ae265e2..fd2bdb8 100644 --- a/src/OHandler.ts +++ b/src/OHandler.ts @@ -47,14 +47,17 @@ export class OHandler { */ public async query(query?: OdataQuery) { try { + this.config.onStart(this); const response: Response[] = await this.getFetch(query); const json = await Promise.all( response.map( async (res) => { if (res.status >= 400) { + this.config.onError(this, res); throw res; } else if (res.ok && res.json) { try { + this.config.onFinish(this, res); const data = await res.json(); return data[this.config.fragment] || data; } catch (ex) { @@ -83,11 +86,14 @@ export class OHandler { */ public async fetch(query?: OdataQuery) { try { + this.config.onStart(this); const fetch = await this.getFetch(query); return fetch.length === 1 ? fetch[0] : fetch; } catch (ex) { + this.config.onError(this, ex); throw ex; } finally { + this.config.onFinish(this); this.requests = []; } } diff --git a/src/OdataConfig.ts b/src/OdataConfig.ts index 31b2deb..acf5ddc 100644 --- a/src/OdataConfig.ts +++ b/src/OdataConfig.ts @@ -1,4 +1,5 @@ import { OdataBatchConfig } from "./OdataBatchConfig"; +import { OHandler } from './OHandler'; export type OdataConfig = RequestInit & { /** @@ -26,4 +27,20 @@ export type OdataConfig = RequestInit & { * Set to true to disable auto polyfilling */ disablePolyfill: boolean; + + /** + * A function which is called on each start of a request + */ + onStart: (oHandler: OHandler) => void; + + /** + * A function which is called when a request has finished + */ + onFinish: (oHandler: OHandler, res?: Response) => void; + + + /** + * A function which is called when a request has a error + */ + onError: (oHandler: OHandler, res: Response) => void; }; diff --git a/src/__snapshots__/o.spec.ts.snap b/src/__snapshots__/o.spec.ts.snap index 2530fb8..3320010 100644 --- a/src/__snapshots__/o.spec.ts.snap +++ b/src/__snapshots__/o.spec.ts.snap @@ -21,6 +21,9 @@ Object { }, }, "mode": "cors", + "onError": [Function], + "onFinish": [Function], + "onStart": [Function], "redirect": "follow", "referrer": "client", "rootUrl": "http://localhost/foo", diff --git a/src/o.spec.ts b/src/o.spec.ts index 1be6431..e09411d 100644 --- a/src/o.spec.ts +++ b/src/o.spec.ts @@ -238,6 +238,17 @@ describe("GET request", () => { expect(typeof data).toBe("object"); }); + test("Request to $top=1 should return a array", async () => { + // given + const resource = "People"; + // when + const data = await oHandler.get(resource).query({ $top: 1 }); + + // expect + expect(Array.isArray(data)).toBe(true); + expect(data.length).toBe(1); + }); + test("Request multiple resources or entities", async () => { // given const resource1 = "People('russellwhyte')"; diff --git a/src/o.ts b/src/o.ts index 9e431f2..eb3aa76 100644 --- a/src/o.ts +++ b/src/o.ts @@ -51,6 +51,9 @@ export function o(rootUrl: string | URL, config?: OdataConfig | any) { mode: "cors", redirect: "follow", referrer: "client", + onStart: () => null, + onError: () => null, + onFinish: () => null }; const mergedConfig = { ...defaultConfigValues, ...config };