diff --git a/package-lock.json b/package-lock.json index e9cd37f..2f9789f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@reactway/api-builder", - "version": "1.0.0-alpha.4", + "version": "1.0.0-alpha.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 4f6ccc3..17573d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@reactway/api-builder", - "version": "1.0.0-alpha.4", + "version": "1.0.0-alpha.5", "description": "An easy api client builder for applications with identity.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/__tests__/api-builder.test.ts b/src/__tests__/api-builder.test.ts index accb8cd..eb3927e 100644 --- a/src/__tests__/api-builder.test.ts +++ b/src/__tests__/api-builder.test.ts @@ -1,9 +1,19 @@ import { ApiBuilder } from "../api-builder"; import fetchMock from "fetch-mock"; -import { ApiRequestBinaryBody, OAuthResponseDto, HttpMethods, ApiRequest } from "../contracts"; +import { ApiRequestBinaryBody, OAuthResponseDto, HttpMethods, ApiRequest, BaseApiRequest, QueryParams } from "../contracts"; import { OAuthIdentity } from "../identities/oauth-identity"; jest.useFakeTimers(); +interface ApiTestClient { + get: (requestDto: BaseApiRequest) => Promise; + post: (requestDto: BaseApiRequest) => Promise; + put: (requestDto: BaseApiRequest) => Promise; + patch: (requestDto: BaseApiRequest) => Promise; + delete: (requestDto: BaseApiRequest) => Promise; + + getItem: () => Promise; +} + const API_TEST_HOST = "https://example.com"; const PATH = "/api"; const PATH_GET = "/get"; @@ -25,6 +35,38 @@ const LOGIN_RESPONSE: OAuthResponseDto = { expires_in: 28800 }; +class ApiClient extends ApiBuilder { + constructor(identity?: OAuthIdentity, queryParams?: QueryParams, queueLimit?: number, usePath?: boolean) { + super({ + host: API_TEST_HOST, + path: usePath === false ? undefined : PATH, + identity: identity, + defaultQueryParams: queryParams, + requestQueueLimit: queueLimit + }); + } + + public async getItem(): Promise { + const request: ApiRequest = { + requestPath: PATH_GET, + method: HttpMethods.GET + }; + return this.get(request); + } +} + +// FIXME: Temporary solution. +// tslint:disable-next-line:no-any +const ApiTestClient: { new (): ApiTestClient } = ApiClient as any; +// FIXME: Temporary solution. +// tslint:disable-next-line:no-any +const ApiTestClientIdentity: { new (identity: OAuthIdentity): ApiTestClient } = ApiClient as any; +const ApiTestClientQueryParams: { new (identity: undefined, queryParams: QueryParams): ApiTestClient } = ApiClient as any; +const ApiTestClientQueueLimits: { new (identity: undefined, queryParams: undefined, queueLimit: number): ApiTestClient } = ApiClient as any; +const ApiTestClientNoPath: { + new (identity: undefined, queryParams: undefined, queueLimit: undefined, usePath: boolean): ApiTestClient; +} = ApiClient as any; + // #region Mocked fetch results. function mockLoginSuccess(): void { fetchMock.post( @@ -123,27 +165,17 @@ afterEach(() => { }); it("make Get request", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiClient = new ApiTestClient(); mockGetSuccess(); - const request: ApiRequest = { - requestPath: PATH_GET, - method: HttpMethods.GET - }; - const getExample = await apiBuilder.get(request); + const getExample = await apiClient.getItem(); expect(getExample.status).toEqual(200); done(); }); it("make Post request", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockPostSuccess(); const getExample = await apiBuilder.post({ @@ -155,10 +187,7 @@ it("make Post request", async done => { }); it("make Post request with body of type string", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockPostSuccess(); const getExample = await apiBuilder.post({ @@ -171,10 +200,7 @@ it("make Post request with body of type string", async done => { }); it("make Post request with body of type object", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockPostSuccess(); const getExample = await apiBuilder.post<{ name: string; surname: string }>({ @@ -190,10 +216,7 @@ it("make Post request with body of type object", async done => { }); it("make Put request", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockPutSuccess(); const getExample = await apiBuilder.put<{ name: string }>({ @@ -208,9 +231,7 @@ it("make Put request", async done => { }); it("make Get request without config path given", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST - }); + const apiBuilder = new ApiTestClientNoPath(undefined, undefined, undefined, false); mockGetPathSuccess(); const getExample = await apiBuilder.get({ @@ -222,11 +243,7 @@ it("make Get request without config path given", async done => { }); it("make Get request with queue limits", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH, - requestQueueLimit: 1 - }); + const apiBuilder = new ApiTestClientQueueLimits(undefined, undefined, 1); mockGetSuccess(); const getExampleOne = apiBuilder.get({ @@ -245,10 +262,7 @@ it("make Get request with queue limits", async done => { }); it("make forced Get request", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockGetSuccess(); const getExample = await apiBuilder.get({ @@ -261,13 +275,10 @@ it("make forced Get request", async done => { }); it("make Get request with api configuration query params", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH, - defaultQueryParams: { - page: 2 - } - }); + const queryParams: QueryParams = { + page: 2 + }; + const apiBuilder = new ApiTestClientQueryParams(undefined, queryParams); mockGetQueryParamsPathSuccess(); const getExample = await apiBuilder.get({ @@ -280,10 +291,7 @@ it("make Get request with api configuration query params", async done => { }); it("make Get request with request query params", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockGetQueryParamsPathSuccess(); const getExample = await apiBuilder.get({ @@ -299,10 +307,7 @@ it("make Get request with request query params", async done => { }); it("make Post request with Uint8Array body", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockPostSuccess(); const getExample = await apiBuilder.post({ @@ -318,10 +323,7 @@ it("make Post request with Uint8Array body", async done => { }); it("make Delete request", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockDeleteSuccess(); const getExample = await apiBuilder.delete<{ id: number }>({ @@ -336,10 +338,7 @@ it("make Delete request", async done => { }); it("make Patch request", async done => { - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH - }); + const apiBuilder = new ApiTestClient(); mockPatchSuccess(); const getExample = await apiBuilder.patch<{ id: number }>({ @@ -363,11 +362,7 @@ it("authenticated request", async done => { mockLoginSuccess(); await identity.login("", ""); - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH, - identity: identity - }); + const apiBuilder = new ApiTestClientIdentity(identity); mockGetSuccess(); const getExample = await apiBuilder.get({ @@ -389,11 +384,7 @@ it("authenticated request but failed", async done => { mockLoginSuccess(); await identity.login("", ""); - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH, - identity: identity - }); + const apiBuilder = new ApiTestClientIdentity(identity); mockGetAuthFailed(); mockLogoutSuccess(); @@ -419,11 +410,7 @@ it("authenticated request and then logout", async done => { mockLoginSuccess(); await identity.login("", ""); - const apiBuilder = new ApiBuilder({ - host: API_TEST_HOST, - path: PATH, - identity: identity - }); + const apiBuilder = new ApiTestClientIdentity(identity); mockGetSuccess(); const getExample = await apiBuilder.get({ diff --git a/src/api-builder.ts b/src/api-builder.ts index 20544d0..f20ae46 100644 --- a/src/api-builder.ts +++ b/src/api-builder.ts @@ -128,7 +128,7 @@ export class ApiBuilder { this.makeRequest(); } - public async get(requestDto: BaseApiRequest): Promise { + protected async get(requestDto: BaseApiRequest): Promise { return new Promise((resolve, reject) => { this.requestsQueue.push({ ...requestDto, @@ -139,7 +139,7 @@ export class ApiBuilder { }); } - public async post(requestDto: BaseApiRequest): Promise { + protected async post(requestDto: BaseApiRequest): Promise { return new Promise((resolve, reject) => { this.requestsQueue.push({ ...requestDto, @@ -150,7 +150,7 @@ export class ApiBuilder { }); } - public async put(requestDto: BaseApiRequest): Promise { + protected async put(requestDto: BaseApiRequest): Promise { return new Promise((resolve, reject) => { this.requestsQueue.push({ ...requestDto, @@ -161,7 +161,7 @@ export class ApiBuilder { }); } - public async patch(requestDto: BaseApiRequest): Promise { + protected async patch(requestDto: BaseApiRequest): Promise { return new Promise((resolve, reject) => { this.requestsQueue.push({ ...requestDto, @@ -172,7 +172,7 @@ export class ApiBuilder { }); } - public async delete(requestDto: BaseApiRequest): Promise { + protected async delete(requestDto: BaseApiRequest): Promise { return new Promise((resolve, reject) => { this.requestsQueue.push({ ...requestDto,