Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.0.0 #97

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Release Notes

## [3.0.0 (2024-XX-XX)](https://github.com/axe-api/axe-api/compare/3.0.0...x.x.x)

- Added navite `RequestInit` type instead of `IRequest`
- Removed `DEFINED_STATUS_CODES` constant.
- Removed `response.json()` calls internally. Request functions return `Response` object now.
- Added `searchParams()` method to add additional URLSearchParams.
- Fixed `addResponse()` interceptor issues.

## [2.1.0 (2024-09-28)](https://github.com/axe-api/axe-api/compare/2.1.0...2.0.1)

- Added missing requests to the Resource object
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "axe-api-client",
"version": "2.1.0",
"version": "3.0.0-rc-3",
"description": "axe-api-client is a native JavaScript client for Axe API servers.",
"type": "module",
"main": "dist/index.cjs",
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ You can send insert, update, delete, and fetch data from Axe API servers without
## ⚙️ Config

```ts
import { api, IRequest } from "axe-api-client";
import { api } from "axe-api-client";

api.setConfig({
baseURL: "https://bookstore.axe-api.com/api/v1",
headers: {},
params: {},
});

api.interceptors.addRequest((request: IRequest) => {
api.interceptors.addRequest((request: RequestInit) => {
return request;
});

Expand Down
2 changes: 0 additions & 2 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ export const SUFFIX_MAP: Record<FullCoditionTypes, string> = {
NULL: "",
"NOT NULL": "$not",
};

export const DEFINED_STATUS_CODES = [200, 204, 400, 404];
18 changes: 8 additions & 10 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Resource } from "./Resource";
import { ConditionTypes, MethodType, QueryFunctionType } from "./Types";

export interface IRequest {
url: string;
method: MethodType;
headers: Record<string, any>;
body: string | undefined;
}
import {
ConditionTypes,
QueryFunctionType,
RequestInterceptorType,
ResponseInterceptorType,
} from "./Types";

export interface IConfig {
baseURL?: string;
Expand All @@ -20,8 +18,8 @@ export interface IInternalConfig extends IConfig {
}

export interface IInterceptors {
requests: any[];
responses: any[];
requests: RequestInterceptorType[];
responses: ResponseInterceptorType[];
}

export interface IPaginate {
Expand Down
45 changes: 27 additions & 18 deletions src/Resource.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { getConfig } from "./Config";
import { DEFINED_STATUS_CODES, SUFFIX_MAP } from "./Constants";
import {
IInternalConfig,
IPaginate,
IPagination,
IQueryable,
IRequest,
} from "./Interfaces";
import { SUFFIX_MAP } from "./Constants";
import { IInternalConfig, IPaginate, IQueryable } from "./Interfaces";
import {
ConditionTypes,
FormBody,
Expand Down Expand Up @@ -41,6 +35,20 @@ export class Resource implements IQueryable {
this.withPath = undefined;
}

/**
* Add additonal URLSearchParams to the request URL
*
* @param searchParams Record<string, string>
* @returns IQueryable
*/
searchParams(searchParams: Record<string, string>) {
for (const key in searchParams) {
this.params.append(key, searchParams[key]);
}

return this;
}

/**
* Select the fields that will be fetched.
*
Expand Down Expand Up @@ -489,7 +497,7 @@ export class Resource implements IQueryable {
* @param query IPaginate
* @returns object
*/
async paginate(query?: IPaginate): Promise<IPagination> {
async paginate(query?: IPaginate): Promise<Response> {
this.params.append("page", query?.page?.toString() ?? "1");
this.params.append("per_page", query?.perPage?.toString() ?? "10");
return this.sendRequest("GET");
Expand Down Expand Up @@ -543,28 +551,29 @@ export class Resource implements IQueryable {
return this.sendRequest("DELETE");
}

private async sendRequest(method: MethodType, data?: FormBody) {
let request: IRequest = {
url: this.toURL(),
private async sendRequest(
method: MethodType,
data?: FormBody,
): Promise<Response> {
let request: RequestInit = {
method,
headers: {
"Content-Type": "application/json",
},
body: data ? JSON.stringify(data) : undefined,
};

// Calls the request interceptors
for (const interceptor of this.config.interceptors.requests) {
request = interceptor(request);
}

const response = await fetch(request.url, request);
// Send the request
let response = await fetch(this.toURL(), request);

// Calls the response interceptors
for (const interceptor of this.config.interceptors.responses) {
interceptor(response);
}

if (DEFINED_STATUS_CODES.includes(response.status)) {
return response.json();
response = interceptor(response);
}

return response;
Expand Down
6 changes: 3 additions & 3 deletions src/Types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { IQueryable, IRequest } from "./Interfaces";
import { IQueryable } from "./Interfaces";

export type FormBody = object | undefined;

export type QueryArray = object[];

export type QueryFunctionType = (query: IQueryable) => IQueryable;

export type RequestInterceptorType = (request: IRequest) => IRequest;
export type RequestInterceptorType = (request: RequestInit) => RequestInit;

export type ResponseInterceptorType = (response: Response) => void;
export type ResponseInterceptorType = (response: Response) => Response;

export type LogicType = "$or" | "$and";

Expand Down
74 changes: 53 additions & 21 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ const BASE_CONFIG = {
};

const mock = (status, data) => {
return jest.fn(() => {
return jest.fn((url) => {
return Promise.resolve({
status,
mock: {
url,
},
json: () => {
return data;
},
Expand Down Expand Up @@ -173,12 +176,15 @@ describe("axe-api-client", () => {
});
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe("https://axe-api.com/api/v1/users"); // NOSONAR
expect(url).toBe("https://axe-api.com/api/v1/users"); // NOSONAR
expect(request.method).toBe("POST");
expect(request.headers["Content-Type"]).toBe("application/json");
expect(request.body).toBe(`{"name":"Karl"}`);
expect(response.id).toBe(100);
const data = await response.json();
expect(response.status).toBe(200);
expect(data.id).toBe(100);
});

test(`update()`, async () => {
Expand All @@ -191,11 +197,13 @@ describe("axe-api-client", () => {
});
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe("https://axe-api.com/api/v1/users/1");
expect(url).toBe("https://axe-api.com/api/v1/users/1");
expect(request.method).toBe("PUT");
expect(request.body).toBe(`{"name":"Karl"}`);
expect(response.id).toBe(100);
const data = await response.json();
expect(data.id).toBe(100);
});

test(`patch()`, async () => {
Expand All @@ -208,11 +216,13 @@ describe("axe-api-client", () => {
});
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe("https://axe-api.com/api/v1/users/1");
expect(url).toBe("https://axe-api.com/api/v1/users/1");
expect(request.method).toBe("PATCH");
expect(request.body).toBe(`{"name":"Karl"}`);
expect(response.id).toBe(100);
const data = await response.json();
expect(data.id).toBe(100);
});

test(`delete()`, async () => {
Expand All @@ -221,11 +231,13 @@ describe("axe-api-client", () => {
const response = await api.resource("users/1").delete();
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe("https://axe-api.com/api/v1/users/1");
expect(url).toBe("https://axe-api.com/api/v1/users/1");
expect(request.method).toBe("DELETE");
expect(request.body).toBe(undefined);
expect(response).toBe(undefined);
const json = await response.json();
expect(json).toBe(undefined);
});

test(`paginate()`, async () => {
Expand All @@ -237,12 +249,12 @@ describe("axe-api-client", () => {
.paginate({ page: 10, perPage: 5 });
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe(
"https://axe-api.com/api/v1/users?page=10&per_page=5",
);
expect(url).toBe("https://axe-api.com/api/v1/users?page=10&per_page=5");
expect(request.method).toBe("GET");
expect(response).toBe(mockResponse);
const json = await response.json();
expect(json).toBe(mockResponse);
});

test(`URL Tests`, async () => {
Expand All @@ -257,12 +269,14 @@ describe("axe-api-client", () => {
.paginate();
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe(
expect(url).toBe(
`https://axe-api.com/api/v1/users?page=1&per_page=10&fields=id&sort=id&q=%5B%7B%22id%22%3A1%7D%5D`,
);
expect(request.method).toBe("GET");
expect(response).toBe(mockResponse);
const json = await response.json();
expect(json).toBe(mockResponse);
});

test(`post()`, async () => {
Expand All @@ -273,11 +287,13 @@ describe("axe-api-client", () => {
const response = await api.resource("users").post(data);
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe("https://axe-api.com/api/v1/users");
expect(url).toBe("https://axe-api.com/api/v1/users");
expect(request.method).toBe("POST");
expect(JSON.parse(request.body).name).toBe(data.name);
expect(response).toBe("RESULT");
const json = await response.json();
expect(json).toBe("RESULT");
});

test(`put()`, async () => {
Expand All @@ -288,11 +304,13 @@ describe("axe-api-client", () => {
const response = await api.resource("users").put(data);
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe("https://axe-api.com/api/v1/users");
expect(url).toBe("https://axe-api.com/api/v1/users");
expect(request.method).toBe("PUT");
expect(JSON.parse(request.body).name).toBe(data.name);
expect(response).toBe("RESULT");
const json = await response.json();
expect(json).toBe("RESULT");
});

test(`patch()`, async () => {
Expand All @@ -303,10 +321,24 @@ describe("axe-api-client", () => {
const response = await api.resource("users").patch(data);
expect(global.fetch.mock.calls.length).toBe(1);

const url = global.fetch.mock.calls[0][0];
const request = global.fetch.mock.calls[0][1];
expect(request.url).toBe("https://axe-api.com/api/v1/users");
expect(url).toBe("https://axe-api.com/api/v1/users");
expect(request.method).toBe("PATCH");
expect(JSON.parse(request.body).name).toBe(data.name);
expect(response).toBe("RESULT");
const json = await response.json();
expect(json).toBe("RESULT");
});

test(`searchParams()`, async () => {
global.fetch = mock(200, "RESULT");

const response = await api
.resource("users")
.searchParams({ myFlag: "true", addUsers: 1 })
.paginate();

expect(response.mock.url.includes("myFlag=true")).toBe(true);
expect(response.mock.url.includes("addUsers=1")).toBe(true);
});
});
Loading
Loading