Skip to content

Commit 2fb3aea

Browse files
author
Eimantas
authored
Hotfix: Methods. (#5)
1 parent 475200a commit 2fb3aea

File tree

4 files changed

+70
-83
lines changed

4 files changed

+70
-83
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reactway/api-builder",
3-
"version": "1.0.0-alpha.4",
3+
"version": "1.0.0-alpha.5",
44
"description": "An easy api client builder for applications with identity.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/__tests__/api-builder.test.ts

Lines changed: 63 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { ApiBuilder } from "../api-builder";
22
import fetchMock from "fetch-mock";
3-
import { ApiRequestBinaryBody, OAuthResponseDto, HttpMethods, ApiRequest } from "../contracts";
3+
import { ApiRequestBinaryBody, OAuthResponseDto, HttpMethods, ApiRequest, BaseApiRequest, QueryParams } from "../contracts";
44
import { OAuthIdentity } from "../identities/oauth-identity";
55
jest.useFakeTimers();
66

7+
interface ApiTestClient {
8+
get: (requestDto: BaseApiRequest<never>) => Promise<Response>;
9+
post: <TBody = {}>(requestDto: BaseApiRequest<TBody>) => Promise<Response>;
10+
put: <TBody = {}>(requestDto: BaseApiRequest<TBody>) => Promise<Response>;
11+
patch: <TBody = {}>(requestDto: BaseApiRequest<TBody>) => Promise<Response>;
12+
delete: <TBody = {}>(requestDto: BaseApiRequest<TBody>) => Promise<Response>;
13+
14+
getItem: () => Promise<Response>;
15+
}
16+
717
const API_TEST_HOST = "https://example.com";
818
const PATH = "/api";
919
const PATH_GET = "/get";
@@ -25,6 +35,38 @@ const LOGIN_RESPONSE: OAuthResponseDto = {
2535
expires_in: 28800
2636
};
2737

38+
class ApiClient extends ApiBuilder {
39+
constructor(identity?: OAuthIdentity, queryParams?: QueryParams, queueLimit?: number, usePath?: boolean) {
40+
super({
41+
host: API_TEST_HOST,
42+
path: usePath === false ? undefined : PATH,
43+
identity: identity,
44+
defaultQueryParams: queryParams,
45+
requestQueueLimit: queueLimit
46+
});
47+
}
48+
49+
public async getItem(): Promise<Response> {
50+
const request: ApiRequest = {
51+
requestPath: PATH_GET,
52+
method: HttpMethods.GET
53+
};
54+
return this.get(request);
55+
}
56+
}
57+
58+
// FIXME: Temporary solution.
59+
// tslint:disable-next-line:no-any
60+
const ApiTestClient: { new (): ApiTestClient } = ApiClient as any;
61+
// FIXME: Temporary solution.
62+
// tslint:disable-next-line:no-any
63+
const ApiTestClientIdentity: { new (identity: OAuthIdentity): ApiTestClient } = ApiClient as any;
64+
const ApiTestClientQueryParams: { new (identity: undefined, queryParams: QueryParams): ApiTestClient } = ApiClient as any;
65+
const ApiTestClientQueueLimits: { new (identity: undefined, queryParams: undefined, queueLimit: number): ApiTestClient } = ApiClient as any;
66+
const ApiTestClientNoPath: {
67+
new (identity: undefined, queryParams: undefined, queueLimit: undefined, usePath: boolean): ApiTestClient;
68+
} = ApiClient as any;
69+
2870
// #region Mocked fetch results.
2971
function mockLoginSuccess(): void {
3072
fetchMock.post(
@@ -123,27 +165,17 @@ afterEach(() => {
123165
});
124166

125167
it("make Get request", async done => {
126-
const apiBuilder = new ApiBuilder({
127-
host: API_TEST_HOST,
128-
path: PATH
129-
});
168+
const apiClient = new ApiTestClient();
130169

131170
mockGetSuccess();
132-
const request: ApiRequest = {
133-
requestPath: PATH_GET,
134-
method: HttpMethods.GET
135-
};
136-
const getExample = await apiBuilder.get(request);
137171

172+
const getExample = await apiClient.getItem();
138173
expect(getExample.status).toEqual(200);
139174
done();
140175
});
141176

142177
it("make Post request", async done => {
143-
const apiBuilder = new ApiBuilder({
144-
host: API_TEST_HOST,
145-
path: PATH
146-
});
178+
const apiBuilder = new ApiTestClient();
147179

148180
mockPostSuccess();
149181
const getExample = await apiBuilder.post({
@@ -155,10 +187,7 @@ it("make Post request", async done => {
155187
});
156188

157189
it("make Post request with body of type string", async done => {
158-
const apiBuilder = new ApiBuilder({
159-
host: API_TEST_HOST,
160-
path: PATH
161-
});
190+
const apiBuilder = new ApiTestClient();
162191

163192
mockPostSuccess();
164193
const getExample = await apiBuilder.post({
@@ -171,10 +200,7 @@ it("make Post request with body of type string", async done => {
171200
});
172201

173202
it("make Post request with body of type object", async done => {
174-
const apiBuilder = new ApiBuilder({
175-
host: API_TEST_HOST,
176-
path: PATH
177-
});
203+
const apiBuilder = new ApiTestClient();
178204

179205
mockPostSuccess();
180206
const getExample = await apiBuilder.post<{ name: string; surname: string }>({
@@ -190,10 +216,7 @@ it("make Post request with body of type object", async done => {
190216
});
191217

192218
it("make Put request", async done => {
193-
const apiBuilder = new ApiBuilder({
194-
host: API_TEST_HOST,
195-
path: PATH
196-
});
219+
const apiBuilder = new ApiTestClient();
197220

198221
mockPutSuccess();
199222
const getExample = await apiBuilder.put<{ name: string }>({
@@ -208,9 +231,7 @@ it("make Put request", async done => {
208231
});
209232

210233
it("make Get request without config path given", async done => {
211-
const apiBuilder = new ApiBuilder({
212-
host: API_TEST_HOST
213-
});
234+
const apiBuilder = new ApiTestClientNoPath(undefined, undefined, undefined, false);
214235

215236
mockGetPathSuccess();
216237
const getExample = await apiBuilder.get({
@@ -222,11 +243,7 @@ it("make Get request without config path given", async done => {
222243
});
223244

224245
it("make Get request with queue limits", async done => {
225-
const apiBuilder = new ApiBuilder({
226-
host: API_TEST_HOST,
227-
path: PATH,
228-
requestQueueLimit: 1
229-
});
246+
const apiBuilder = new ApiTestClientQueueLimits(undefined, undefined, 1);
230247

231248
mockGetSuccess();
232249
const getExampleOne = apiBuilder.get({
@@ -245,10 +262,7 @@ it("make Get request with queue limits", async done => {
245262
});
246263

247264
it("make forced Get request", async done => {
248-
const apiBuilder = new ApiBuilder({
249-
host: API_TEST_HOST,
250-
path: PATH
251-
});
265+
const apiBuilder = new ApiTestClient();
252266

253267
mockGetSuccess();
254268
const getExample = await apiBuilder.get({
@@ -261,13 +275,10 @@ it("make forced Get request", async done => {
261275
});
262276

263277
it("make Get request with api configuration query params", async done => {
264-
const apiBuilder = new ApiBuilder({
265-
host: API_TEST_HOST,
266-
path: PATH,
267-
defaultQueryParams: {
268-
page: 2
269-
}
270-
});
278+
const queryParams: QueryParams = {
279+
page: 2
280+
};
281+
const apiBuilder = new ApiTestClientQueryParams(undefined, queryParams);
271282

272283
mockGetQueryParamsPathSuccess();
273284
const getExample = await apiBuilder.get({
@@ -280,10 +291,7 @@ it("make Get request with api configuration query params", async done => {
280291
});
281292

282293
it("make Get request with request query params", async done => {
283-
const apiBuilder = new ApiBuilder({
284-
host: API_TEST_HOST,
285-
path: PATH
286-
});
294+
const apiBuilder = new ApiTestClient();
287295

288296
mockGetQueryParamsPathSuccess();
289297
const getExample = await apiBuilder.get({
@@ -299,10 +307,7 @@ it("make Get request with request query params", async done => {
299307
});
300308

301309
it("make Post request with Uint8Array body", async done => {
302-
const apiBuilder = new ApiBuilder({
303-
host: API_TEST_HOST,
304-
path: PATH
305-
});
310+
const apiBuilder = new ApiTestClient();
306311

307312
mockPostSuccess();
308313
const getExample = await apiBuilder.post<ApiRequestBinaryBody>({
@@ -318,10 +323,7 @@ it("make Post request with Uint8Array body", async done => {
318323
});
319324

320325
it("make Delete request", async done => {
321-
const apiBuilder = new ApiBuilder({
322-
host: API_TEST_HOST,
323-
path: PATH
324-
});
326+
const apiBuilder = new ApiTestClient();
325327

326328
mockDeleteSuccess();
327329
const getExample = await apiBuilder.delete<{ id: number }>({
@@ -336,10 +338,7 @@ it("make Delete request", async done => {
336338
});
337339

338340
it("make Patch request", async done => {
339-
const apiBuilder = new ApiBuilder({
340-
host: API_TEST_HOST,
341-
path: PATH
342-
});
341+
const apiBuilder = new ApiTestClient();
343342

344343
mockPatchSuccess();
345344
const getExample = await apiBuilder.patch<{ id: number }>({
@@ -363,11 +362,7 @@ it("authenticated request", async done => {
363362
mockLoginSuccess();
364363
await identity.login("", "");
365364

366-
const apiBuilder = new ApiBuilder({
367-
host: API_TEST_HOST,
368-
path: PATH,
369-
identity: identity
370-
});
365+
const apiBuilder = new ApiTestClientIdentity(identity);
371366

372367
mockGetSuccess();
373368
const getExample = await apiBuilder.get({
@@ -389,11 +384,7 @@ it("authenticated request but failed", async done => {
389384
mockLoginSuccess();
390385
await identity.login("", "");
391386

392-
const apiBuilder = new ApiBuilder({
393-
host: API_TEST_HOST,
394-
path: PATH,
395-
identity: identity
396-
});
387+
const apiBuilder = new ApiTestClientIdentity(identity);
397388

398389
mockGetAuthFailed();
399390
mockLogoutSuccess();
@@ -419,11 +410,7 @@ it("authenticated request and then logout", async done => {
419410
mockLoginSuccess();
420411
await identity.login("", "");
421412

422-
const apiBuilder = new ApiBuilder({
423-
host: API_TEST_HOST,
424-
path: PATH,
425-
identity: identity
426-
});
413+
const apiBuilder = new ApiTestClientIdentity(identity);
427414

428415
mockGetSuccess();
429416
const getExample = await apiBuilder.get({

src/api-builder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class ApiBuilder {
128128
this.makeRequest();
129129
}
130130

131-
public async get(requestDto: BaseApiRequest<never>): Promise<Response> {
131+
protected async get(requestDto: BaseApiRequest<never>): Promise<Response> {
132132
return new Promise<Response>((resolve, reject) => {
133133
this.requestsQueue.push({
134134
...requestDto,
@@ -139,7 +139,7 @@ export class ApiBuilder {
139139
});
140140
}
141141

142-
public async post<TBody = {}>(requestDto: BaseApiRequest<TBody>): Promise<Response> {
142+
protected async post<TBody = {}>(requestDto: BaseApiRequest<TBody>): Promise<Response> {
143143
return new Promise<Response>((resolve, reject) => {
144144
this.requestsQueue.push({
145145
...requestDto,
@@ -150,7 +150,7 @@ export class ApiBuilder {
150150
});
151151
}
152152

153-
public async put<TBody = {}>(requestDto: BaseApiRequest<TBody>): Promise<Response> {
153+
protected async put<TBody = {}>(requestDto: BaseApiRequest<TBody>): Promise<Response> {
154154
return new Promise<Response>((resolve, reject) => {
155155
this.requestsQueue.push({
156156
...requestDto,
@@ -161,7 +161,7 @@ export class ApiBuilder {
161161
});
162162
}
163163

164-
public async patch<TBody = {}>(requestDto: BaseApiRequest<TBody>): Promise<Response> {
164+
protected async patch<TBody = {}>(requestDto: BaseApiRequest<TBody>): Promise<Response> {
165165
return new Promise<Response>((resolve, reject) => {
166166
this.requestsQueue.push({
167167
...requestDto,
@@ -172,7 +172,7 @@ export class ApiBuilder {
172172
});
173173
}
174174

175-
public async delete<TBody = {}>(requestDto: BaseApiRequest<TBody>): Promise<Response> {
175+
protected async delete<TBody = {}>(requestDto: BaseApiRequest<TBody>): Promise<Response> {
176176
return new Promise<Response>((resolve, reject) => {
177177
this.requestsQueue.push({
178178
...requestDto,

0 commit comments

Comments
 (0)