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

feature: added custom axios request #981

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions templates/base/http-clients/axios-http-client.ejs
Original file line number Diff line number Diff line change
@@ -28,6 +28,9 @@ export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequest
securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
secure?: boolean;
format?: ResponseType;
customAction?: <T>(
action: () => Promise<AxiosResponse<T>>,
) => Promise<AxiosResponse<T>>;
}

export enum ContentType {
@@ -43,12 +46,13 @@ export class HttpClient<SecurityDataType = unknown> {
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
private secure?: boolean;
private format?: ResponseType;

constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
private customAction?: ApiConfig<SecurityDataType>["customAction"];
constructor({ securityWorker, secure, format, customAction, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
this.secure = secure;
this.format = format;
this.securityWorker = securityWorker;
this.customAction = customAction;
}

public setSecurityData = (data: SecurityDataType | null) => {
@@ -123,7 +127,7 @@ export class HttpClient<SecurityDataType = unknown> {
body = JSON.stringify(body);
}

return this.instance.request({
const action = () => this.instance.request({
...requestParams,
headers: {
...(requestParams.headers || {}),
@@ -138,5 +142,7 @@ export class HttpClient<SecurityDataType = unknown> {
<% } else { %>
});
<% } %>

return this.customAction ? this.customAction(action) : action();
};
}

Unchanged files with check annotations Beta

encoding: "utf8",
});
expect(content).toMatchSnapshot();

Check failure on line 34 in tests/spec/axios/basic.test.ts

GitHub Actions / build-and-test

tests/spec/axios/basic.test.ts > basic > --axios option

Error: Snapshot `basic > --axios option 1` mismatched - Expected + Received @@ -9,288 +9,170 @@ * --------------------------------------------------------------- */ /** A user or organization */ export interface Actor { - avatar_url?: string; - bio?: string; - /** The website URL from the profile page */ - blog?: string; - collaborators?: number; - company?: string; - /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ - created_at?: string; - disk_usage?: number; - /** Note: The returned email is the user’s publicly visible email address (or null if the user has not specified a public email address in their profile). */ - email?: string; - followers?: number; - followers_url?: string; - following?: number; - following_url?: string; - gists_url?: string; - gravatar_id?: string; - hireable?: boolean; - html_url?: string; - id?: number; - location?: string; - /** The account username */ - login?: string; - /** The full account name */ - name?: string; - organizations_url?: string; - owned_private_repos?: number; - plan?: { + avatar_url?: string; + bio?: string; + /** The website URL from the profile page */ + blog?: string; collaborators?: number; + company?: string; + /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ + created_at?: string; + disk_usage?: number; + /** Note: The returned email is the user’s publicly visible email address (or null if the user has not specified a public email address in their profile). */ + email?: string; + followers?: number; + followers_url?: string; + following?: number; + following_url?: string; + gists_url?: string; + gravatar_id?: string; + hireable?: boolean; + html_url?: string; + id?: number; + location?: string; + /** The account username */ + login?: string; + /** The full account name */ name?: string; - private_repos?: number; - space?: number; - }; - private_gists?: number; - public_gists?: number; - public_repos?: number; - starred_url?: string; - subscriptions_url?: string; - total_private_repos?: number; - type?: "User" | "Organization"; - /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ - updated_at?: string; - url?: string; + organizations_url?: string; + owned_private_repos?: number; + plan?: { + collaborators?: number; + name?: string; + private_repos?: number; + space?: number; + }; + private_gists?: number; + public_gists?: number; + public_repos?: number; + starred_url?: string; + subscriptions_url?: string; + total_private_repos?: number; + type?: "User" | "Organization"; + /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ + updated_at?: string; + url?: string; } export interface Asset { - content_type?: string; - created_at?: string; - download_count?: number; - id?: number; - label?: string; - name?: string; - size?: number; - state?: string; - updated_at?: string; - /** A GitHub user */ - uploader?: User; - url?: string; + content_type?: string; + created_at?: string; + download_count?: number; + id?: number; + label?: string; + name?: string; + size?: number; + state?: string; + updated_at?: string; + /** A GitHub user */ + uploader?: User; + url?: string; } export interface AssetPatch { - label?: string; - name: string; + label?: string; + name: string; } export type Assets = Asset[]; export type Assignees = User[]; export interface Blob { - content?: string; - encoding?: "utf-8" | "base64"; - sha?: string; - size?: number; + content?: string; + encoding?: "utf-8" | "base64"; + sha?: string; + size?: number; } export interface Blobs { - sha?: string; + sha?: string; } export interface Branch { - _links?: { - html?: string; - self?: string; - }; - commit?: { - /** A GitHub user */ - author?: User; + _links?: { + html?: string; + self?: string; + }; commit?: { - author?: { - /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ - date?: string; - email?: string; - name?: string; - }; - committer?: { - /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ -
});
});
encoding: "utf8",
});
expect(content).toMatchSnapshot();

Check failure on line 35 in tests/spec/axiosSingleHttpClient/basic.test.ts

GitHub Actions / build-and-test

tests/spec/axiosSingleHttpClient/basic.test.ts > basic > --axios --single-http-client

Error: Snapshot `basic > --axios --single-http-client 1` mismatched - Expected + Received @@ -9,288 +9,170 @@ * --------------------------------------------------------------- */ /** A user or organization */ export interface Actor { - avatar_url?: string; - bio?: string; - /** The website URL from the profile page */ - blog?: string; - collaborators?: number; - company?: string; - /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ - created_at?: string; - disk_usage?: number; - /** Note: The returned email is the user’s publicly visible email address (or null if the user has not specified a public email address in their profile). */ - email?: string; - followers?: number; - followers_url?: string; - following?: number; - following_url?: string; - gists_url?: string; - gravatar_id?: string; - hireable?: boolean; - html_url?: string; - id?: number; - location?: string; - /** The account username */ - login?: string; - /** The full account name */ - name?: string; - organizations_url?: string; - owned_private_repos?: number; - plan?: { + avatar_url?: string; + bio?: string; + /** The website URL from the profile page */ + blog?: string; collaborators?: number; + company?: string; + /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ + created_at?: string; + disk_usage?: number; + /** Note: The returned email is the user’s publicly visible email address (or null if the user has not specified a public email address in their profile). */ + email?: string; + followers?: number; + followers_url?: string; + following?: number; + following_url?: string; + gists_url?: string; + gravatar_id?: string; + hireable?: boolean; + html_url?: string; + id?: number; + location?: string; + /** The account username */ + login?: string; + /** The full account name */ name?: string; - private_repos?: number; - space?: number; - }; - private_gists?: number; - public_gists?: number; - public_repos?: number; - starred_url?: string; - subscriptions_url?: string; - total_private_repos?: number; - type?: "User" | "Organization"; - /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ - updated_at?: string; - url?: string; + organizations_url?: string; + owned_private_repos?: number; + plan?: { + collaborators?: number; + name?: string; + private_repos?: number; + space?: number; + }; + private_gists?: number; + public_gists?: number; + public_repos?: number; + starred_url?: string; + subscriptions_url?: string; + total_private_repos?: number; + type?: "User" | "Organization"; + /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ + updated_at?: string; + url?: string; } export interface Asset { - content_type?: string; - created_at?: string; - download_count?: number; - id?: number; - label?: string; - name?: string; - size?: number; - state?: string; - updated_at?: string; - /** A GitHub user */ - uploader?: User; - url?: string; + content_type?: string; + created_at?: string; + download_count?: number; + id?: number; + label?: string; + name?: string; + size?: number; + state?: string; + updated_at?: string; + /** A GitHub user */ + uploader?: User; + url?: string; } export interface AssetPatch { - label?: string; - name: string; + label?: string; + name: string; } export type Assets = Asset[]; export type Assignees = User[]; export interface Blob { - content?: string; - encoding?: "utf-8" | "base64"; - sha?: string; - size?: number; + content?: string; + encoding?: "utf-8" | "base64"; + sha?: string; + size?: number; } export interface Blobs { - sha?: string; + sha?: string; } export interface Branch { - _links?: { - html?: string; - self?: string; - }; - commit?: { - /** A GitHub user */ - author?: User; + _links?: { + html?: string; + self?: string; + }; commit?: { - author?: { - /** ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ */ - date?: string; - email?: string; - name?: string; - }; - committer?: { - /** ISO 8601 format: YYYY-MM-DDT
});
});
encoding: "utf8",
});
expect(content).toMatchSnapshot();

Check failure on line 34 in tests/spec/js/basic.test.ts

GitHub Actions / build-and-test

tests/spec/js/basic.test.ts > basic > --js --axios

Error: Snapshot `basic > --js --axios 1` mismatched - Expected + Received @@ -21,15 +21,17 @@ instance; securityData = null; securityWorker; secure; format; - constructor({ securityWorker, secure, format, ...axiosConfig } = {}) { + customAction; + constructor({ securityWorker, secure, format, customAction, ...axiosConfig } = {}) { this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "https://api.github.com" }); this.secure = secure; this.format = format; this.securityWorker = securityWorker; + this.customAction = customAction; } setSecurityData = (data) => { this.securityData = data; }; mergeRequestParams(params1, params2) { @@ -78,21 +80,23 @@ body = this.createFormData(body); } if (type === ContentType.Text && body && body !== null && typeof body !== "string") { body = JSON.stringify(body); } - return this.instance.request({ - ...requestParams, - headers: { - ...(requestParams.headers || {}), - ...(type ? { "Content-Type": type } : {}), - }, - params: query, - responseType: responseFormat, - data: body, - url: path, - }); + const action = () => + this.instance.request({ + ...requestParams, + headers: { + ...(requestParams.headers || {}), + ...(type ? { "Content-Type": type } : {}), + }, + params: query, + responseType: responseFormat, + data: body, + url: path, + }); + return this.customAction ? this.customAction(action) : action(); }; } /** * @title GitHub * @Version v3 ❯ tests/spec/js/basic.test.ts:34:21
});
});