@toktokhan-dev/cli-plugin-gen-api-react-query 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/index.d.ts +62 -0
  2. package/dist/index.js +196 -0
  3. package/package.json +40 -0
  4. package/templates/custom-axios/api.eta +176 -0
  5. package/templates/custom-axios/data-contracts.eta +76 -0
  6. package/templates/custom-axios/http-client.eta +144 -0
  7. package/templates/custom-axios/procedure-call.eta +133 -0
  8. package/templates/custom-fetch/api.eta +164 -0
  9. package/templates/custom-fetch/data-contracts.eta +76 -0
  10. package/templates/custom-fetch/http-client.eta +224 -0
  11. package/templates/custom-fetch/procedure-call.eta +151 -0
  12. package/templates/default/README.md +17 -0
  13. package/templates/default/base/README.md +8 -0
  14. package/templates/default/base/data-contract-jsdoc.ejs +37 -0
  15. package/templates/default/base/data-contracts.ejs +28 -0
  16. package/templates/default/base/enum-data-contract.ejs +12 -0
  17. package/templates/default/base/http-client.ejs +3 -0
  18. package/templates/default/base/http-clients/axios-http-client.ejs +138 -0
  19. package/templates/default/base/http-clients/fetch-http-client.ejs +224 -0
  20. package/templates/default/base/interface-data-contract.ejs +10 -0
  21. package/templates/default/base/object-field-jsdoc.ejs +28 -0
  22. package/templates/default/base/route-docs.ejs +30 -0
  23. package/templates/default/base/route-name.ejs +43 -0
  24. package/templates/default/base/route-type.ejs +22 -0
  25. package/templates/default/base/type-data-contract.ejs +15 -0
  26. package/templates/default/default/README.md +7 -0
  27. package/templates/default/default/api.ejs +65 -0
  28. package/templates/default/default/procedure-call.ejs +100 -0
  29. package/templates/default/default/route-types.ejs +28 -0
  30. package/templates/default/modular/README.md +7 -0
  31. package/templates/default/modular/api.ejs +28 -0
  32. package/templates/default/modular/procedure-call.ejs +100 -0
  33. package/templates/default/modular/route-types.ejs +18 -0
  34. package/templates/my/param-serializer-by.eta +41 -0
  35. package/templates/my/react-query-hook.eta +171 -0
  36. package/templates/my/react-query-key.eta +46 -0
  37. package/templates/my/react-query-type.eta +61 -0
  38. package/templates/my/util-types.eta +32 -0
@@ -0,0 +1,151 @@
1
+ <%
2
+ const { utils, route, config, myConfig } = it;
3
+ const { requestBodyInfo, responseBodyInfo, specificArgNameResolver } = route;
4
+ const { _, getInlineParseContent, getParseContent, parseSchema, getComponentByRef, require } = utils;
5
+ const { parameters, path, method, payload, query, formData, security, requestParams } = route.request;
6
+ const { type, errorType, contentTypes } = route.response;
7
+ const { HTTP_CLIENT, RESERVED_REQ_PARAMS_ARG_NAMES } = config.constants;
8
+ const routeDocs = includeFile("@base/route-docs", { config, route, utils });
9
+ const queryName = (query && query.name) || "query";
10
+ const pathParams = _.values(parameters);
11
+ const pathParamsNames = _.map(pathParams, "name");
12
+
13
+ const isFetchTemplate = config.httpClientType === HTTP_CLIENT.FETCH;
14
+
15
+ const requestConfigParam = {
16
+ name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
17
+ optional: true,
18
+ type: "RequestParams",
19
+ defaultValue: "{}",
20
+ }
21
+ const argToTmpl = ({ name, optional, type, defaultValue }) => {
22
+ return `${name}${optional ? '?' : ''}: ${name === "data" ? `DeepOmitReadOnly<${type}>` :type}`;
23
+ }
24
+
25
+
26
+ const rawWrapperArgs = config.extractRequestParams ?
27
+ _.compact([
28
+ requestParams && {
29
+ name: pathParams.length ? `{ ${_.join(pathParamsNames, ", ")}, ...${queryName} }` : queryName,
30
+ optional: false,
31
+ type: getInlineParseContent(requestParams),
32
+ },
33
+ ...(!requestParams ? pathParams : []),
34
+ payload,
35
+ requestConfigParam,
36
+ ]) :
37
+ _.compact([
38
+ ...pathParams,
39
+ query,
40
+ payload,
41
+ requestConfigParam,
42
+ ])
43
+
44
+ const wrapperArgs = _
45
+ // Sort by optionality
46
+ .sortBy(rawWrapperArgs, [o => o.optional])
47
+ .map(argToTmpl)
48
+ .join('; ');
49
+
50
+ const isOptialnalVariabels = _
51
+ // Find optional value
52
+ .filter(rawWrapperArgs, o => o.optional).length === rawWrapperArgs.length;
53
+
54
+ const conditionalVriablesText = isOptialnalVariabels? "variables?" : "variables";
55
+ const repalceTarget = "${" + conditionalVriablesText + ".";
56
+ const pathWithVariables = path.replace(/\$\{/g, repalceTarget);
57
+
58
+
59
+
60
+ // RequestParams["type"]
61
+ const requestContentKind = {
62
+ "JSON": "ContentType.Json",
63
+ "URL_ENCODED": "ContentType.UrlEncoded",
64
+ "FORM_DATA": "ContentType.FormData",
65
+ "TEXT": "ContentType.Text",
66
+ }
67
+ // RequestParams["format"]
68
+ const responseContentKind = {
69
+ "JSON": '"json"',
70
+ "IMAGE": '"blob"',
71
+ "FORM_DATA": isFetchTemplate ? '"formData"' : '"document"'
72
+ }
73
+
74
+ const explodedParams = (properties) => {
75
+ return Object.keys(properties).filter((key) => {
76
+ const item = properties[key]
77
+ return item.type === "array" && item.explode === true
78
+ })
79
+ }
80
+
81
+ const bodyTmpl = _.get(payload, "name") || null;
82
+ const queryTmpl = (query != null && queryName) || null;
83
+ const bodyContentKindTmpl = requestContentKind[requestBodyInfo.contentKind] || null;
84
+ const responseFormatTmpl = responseContentKind[responseBodyInfo.success && responseBodyInfo.success.schema && responseBodyInfo.success.schema.contentKind] || null;
85
+ const securityTmpl = security ? 'true' : null;
86
+ const paramsSerializerTmpl = (() => {
87
+ if (!requestParams?.properties) return;
88
+ const exploded = explodedParams(requestParams.properties);
89
+
90
+ if (!exploded.length) return;
91
+ const configs = exploded.map((key) => `"${key}" : "repeat"`)
92
+
93
+ return `paramsSerializerBy({ ${configs.join(",")} })`
94
+ })()
95
+
96
+
97
+
98
+ const describeReturnType = () => {
99
+ if (!config.toJS) return "";
100
+
101
+ switch(config.httpClientType) {
102
+ case HTTP_CLIENT.AXIOS: {
103
+ return `Promise<AxiosResponse<${type}>>`
104
+ }
105
+ default: {
106
+ return `Promise<HttpResponse<${type}, ${errorType}>`
107
+ }
108
+ }
109
+ }
110
+
111
+ const generatePathSegments = (path) => {
112
+ const segments = [];
113
+ const parts = path.split('/').filter(part => part.length > 0);
114
+
115
+ let currentPath = '';
116
+ for (const part of parts) {
117
+ currentPath += `/${part}`;
118
+ segments.push(`${currentPath}`);
119
+ }
120
+
121
+ return segments;
122
+ }
123
+
124
+ const pathSegments = generatePathSegments(pathWithVariables).map(segment => `\`${segment}\``).join(', ');
125
+
126
+ %>
127
+ /**
128
+ <%~ routeDocs.description %>
129
+
130
+ *<% /* Here you can add some other JSDoc tags */ %>
131
+
132
+ <%~ routeDocs.lines %>
133
+
134
+ */
135
+
136
+ <%~ route.routeName.usage %> = (<%~ conditionalVriablesText %> :{<%~ wrapperArgs %>})<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
137
+ <%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
138
+ path: `<%~ pathWithVariables %>`,
139
+ method: '<%~ _.upperCase(method) %>',
140
+ <%~ queryTmpl ? `query: ${conditionalVriablesText}.${queryTmpl},` : '' %>
141
+ <%~ bodyTmpl ? `body: ${conditionalVriablesText}.${bodyTmpl},` : '' %>
142
+ <%~ securityTmpl ? `secure: ${securityTmpl},` : '' %>
143
+ <%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
144
+ <%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
145
+ <%~ paramsSerializerTmpl ? `paramsSerializer: ${paramsSerializerTmpl},` : '' %>
146
+ ...<%~ `${conditionalVriablesText}`%>.<%~ _.get(requestConfigParam, "name") %>,
147
+ next: {
148
+ ...<%~ `${conditionalVriablesText}`%>.<%~ _.get(requestConfigParam, "name") %>?.next,
149
+ tags: [<%= pathSegments %>],
150
+ },
151
+ })
@@ -0,0 +1,17 @@
1
+ # swagger-typescript-api
2
+
3
+ # templates
4
+
5
+ Templates:
6
+ - `api.ejs` - *(generates file)* Api class module (locations: [default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/api.ejs), [modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/api.ejs))
7
+ - `data-contracts.ejs` - *(generates file)* all types (data contracts) from swagger schema (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contracts.ejs))
8
+ - `http-client.ejs` - *(generates file)* HttpClient class module (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-client.ejs))
9
+ - `procedure-call.ejs` - *(subtemplate)* route in Api class (locations: [default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/procedure-call.ejs), [modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/procedure-call.ejs))
10
+ - `route-docs.ejs` - *(generates file)* documentation for route in Api class (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-docs.ejs))
11
+ - `route-name.ejs` - *(subtemplate)* route name for route in Api class (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-name.ejs))
12
+ - `route-type.ejs` - *(`--route-types` option)* *(subtemplate)* (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-type.ejs))
13
+ - `route-types.ejs` - *(`--route-types` option)* *(subtemplate)* (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-types.ejs)) - `data-contract-jsdoc.ejs` - *(subtemplate)* generates JSDOC for data contract (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contract-jsdoc.ejs))
14
+
15
+ [//]: # (- `enum-data-contract.ejs` - *&#40;subtemplate&#41;* generates `enum` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/enum-data-contract.ejs&#41;&#41;)
16
+ [//]: # (- `interface-data-contract.ejs` - *&#40;subtemplate&#41;* generates `interface` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/interface-data-contract.ejs&#41;&#41;)
17
+ [//]: # (- `type-data-contract.ejs` - *&#40;subtemplate&#41;* generates `type` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/type-data-contract.ejs&#41;&#41;)
@@ -0,0 +1,8 @@
1
+ # swagger-typescript-api
2
+
3
+ # templates/base
4
+
5
+ This templates use both for multiple api files and single api file
6
+
7
+
8
+ path prefix `@base`
@@ -0,0 +1,37 @@
1
+ <%
2
+ const { data, utils } = it;
3
+ const { formatDescription, require, _ } = utils;
4
+
5
+ const stringify = (value) => _.isObject(value) ? JSON.stringify(value) : _.isString(value) ? `"${value}"` : value
6
+
7
+ const jsDocLines = _.compact([
8
+ data.title,
9
+ data.description && formatDescription(data.description),
10
+ !_.isUndefined(data.deprecated) && data.deprecated && '@deprecated',
11
+ !_.isUndefined(data.format) && `@format ${data.format}`,
12
+ !_.isUndefined(data.minimum) && `@min ${data.minimum}`,
13
+ !_.isUndefined(data.multipleOf) && `@multipleOf ${data.multipleOf}`,
14
+ !_.isUndefined(data.exclusiveMinimum) && `@exclusiveMin ${data.exclusiveMinimum}`,
15
+ !_.isUndefined(data.maximum) && `@max ${data.maximum}`,
16
+ !_.isUndefined(data.minLength) && `@minLength ${data.minLength}`,
17
+ !_.isUndefined(data.maxLength) && `@maxLength ${data.maxLength}`,
18
+ !_.isUndefined(data.exclusiveMaximum) && `@exclusiveMax ${data.exclusiveMaximum}`,
19
+ !_.isUndefined(data.maxItems) && `@maxItems ${data.maxItems}`,
20
+ !_.isUndefined(data.minItems) && `@minItems ${data.minItems}`,
21
+ !_.isUndefined(data.uniqueItems) && `@uniqueItems ${data.uniqueItems}`,
22
+ !_.isUndefined(data.default) && `@default ${stringify(data.default)}`,
23
+ !_.isUndefined(data.pattern) && `@pattern ${data.pattern}`,
24
+ !_.isUndefined(data.example) && `@example ${stringify(data.example)}`
25
+ ]).join('\n').split('\n');
26
+ %>
27
+ <% if (jsDocLines.every(_.isEmpty)) { %>
28
+ <% } else if (jsDocLines.length === 1) { %>
29
+ /** <%~ jsDocLines[0] %> */
30
+ <% } else if (jsDocLines.length) { %>
31
+ /**
32
+ <% for (jsDocLine of jsDocLines) { %>
33
+ * <%~ jsDocLine %>
34
+
35
+ <% } %>
36
+ */
37
+ <% } %>
@@ -0,0 +1,28 @@
1
+ <%
2
+ const { modelTypes, utils, config } = it;
3
+ const { formatDescription, require, _, Ts } = utils;
4
+
5
+
6
+ const dataContractTemplates = {
7
+ enum: (contract) => {
8
+ return `enum ${contract.name} {\r\n${contract.content} \r\n }`;
9
+ },
10
+ interface: (contract) => {
11
+ return `interface ${contract.name} {\r\n${contract.content}}`;
12
+ },
13
+ type: (contract) => {
14
+ return `type ${contract.name} = ${contract.content}`;
15
+ },
16
+ }
17
+ %>
18
+
19
+ <% if (config.internalTemplateOptions.addUtilRequiredKeysType) { %>
20
+ type <%~ config.Ts.CodeGenKeyword.UtilRequiredKeys %><T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
21
+ <% } %>
22
+
23
+ <% modelTypes.forEach((contract) => { %>
24
+ <%~ includeFile('@base/data-contract-jsdoc.ejs', { ...it, data: { ...contract, ...contract.typeData } }) %>
25
+ export <%~ (dataContractTemplates[contract.typeIdentifier] || dataContractTemplates.type)(contract) %>
26
+
27
+
28
+ <% }) %>
@@ -0,0 +1,12 @@
1
+ <%
2
+ const { contract, utils, config } = it;
3
+ const { formatDescription, require, _ } = utils;
4
+ const { name, $content } = contract;
5
+ %>
6
+ <% if (config.generateUnionEnums) { %>
7
+ export type <%~ name %> = <%~ _.map($content, ({ value }) => value).join(" | ") %>
8
+ <% } else { %>
9
+ export enum <%~ name %> {
10
+ <%~ _.map($content, ({ key, value }) => `${key} = ${value}`).join(",\n") %>
11
+ }
12
+ <% } %>
@@ -0,0 +1,3 @@
1
+ <% const { config } = it; %>
2
+ <% /* https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-clients/ */ %>
3
+ <%~ includeFile(`@base/http-clients/${config.httpClientType}-http-client`, it) %>
@@ -0,0 +1,138 @@
1
+ <%
2
+ const { apiConfig, generateResponses, config } = it;
3
+ %>
4
+
5
+ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType, HeadersDefaults } from "axios";
6
+
7
+ export type QueryParamsType = Record<string | number, any>;
8
+
9
+ export interface FullRequestParams extends Omit<AxiosRequestConfig, "data" | "params" | "url" | "responseType"> {
10
+ /** set parameter to `true` for call `securityWorker` for this request */
11
+ secure?: boolean;
12
+ /** request path */
13
+ path: string;
14
+ /** content type of request body */
15
+ type?: ContentType;
16
+ /** query params */
17
+ query?: QueryParamsType;
18
+ /** format of response (i.e. response.json() -> format: "json") */
19
+ format?: ResponseType;
20
+ /** request body */
21
+ body?: unknown;
22
+ }
23
+
24
+ export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
25
+
26
+ export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequestConfig, "data" | "cancelToken"> {
27
+ securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
28
+ secure?: boolean;
29
+ format?: ResponseType;
30
+ }
31
+
32
+ export enum ContentType {
33
+ Json = "application/json",
34
+ FormData = "multipart/form-data",
35
+ UrlEncoded = "application/x-www-form-urlencoded",
36
+ Text = "text/plain",
37
+ }
38
+
39
+ export class HttpClient<SecurityDataType = unknown> {
40
+ public instance: AxiosInstance;
41
+ private securityData: SecurityDataType | null = null;
42
+ private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
43
+ private secure?: boolean;
44
+ private format?: ResponseType;
45
+
46
+ constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
47
+ this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
48
+ this.secure = secure;
49
+ this.format = format;
50
+ this.securityWorker = securityWorker;
51
+ }
52
+
53
+ public setSecurityData = (data: SecurityDataType | null) => {
54
+ this.securityData = data
55
+ }
56
+
57
+ protected mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig {
58
+ const method = params1.method || (params2 && params2.method)
59
+
60
+ return {
61
+ ...this.instance.defaults,
62
+ ...params1,
63
+ ...(params2 || {}),
64
+ headers: {
65
+ ...((method && this.instance.defaults.headers[method.toLowerCase() as keyof HeadersDefaults]) || {}),
66
+ ...(params1.headers || {}),
67
+ ...((params2 && params2.headers) || {}),
68
+ },
69
+ };
70
+ }
71
+
72
+ protected stringifyFormItem(formItem: unknown) {
73
+ if (typeof formItem === "object" && formItem !== null) {
74
+ return JSON.stringify(formItem);
75
+ } else {
76
+ return `${formItem}`;
77
+ }
78
+ }
79
+
80
+ protected createFormData(input: Record<string, unknown>): FormData {
81
+ return Object.keys(input || {}).reduce((formData, key) => {
82
+ const property = input[key];
83
+ const propertyContent: any[] = (property instanceof Array) ? property : [property]
84
+
85
+ for (const formItem of propertyContent) {
86
+ const isFileType = formItem instanceof Blob || formItem instanceof File;
87
+ formData.append(
88
+ key,
89
+ isFileType ? formItem : this.stringifyFormItem(formItem)
90
+ );
91
+ }
92
+
93
+ return formData;
94
+ }, new FormData());
95
+ }
96
+
97
+ public request = async <T = any, _E = any>({
98
+ secure,
99
+ path,
100
+ type,
101
+ query,
102
+ format,
103
+ body,
104
+ ...params
105
+ <% if (config.unwrapResponseData) { %>
106
+ }: FullRequestParams): Promise<T> => {
107
+ <% } else { %>
108
+ }: FullRequestParams): Promise<AxiosResponse<T>> => {
109
+ <% } %>
110
+ const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
111
+ const requestParams = this.mergeRequestParams(params, secureParams);
112
+ const responseFormat = (format || this.format) || undefined;
113
+
114
+ if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
115
+ body = this.createFormData(body as Record<string, unknown>);
116
+ }
117
+
118
+ if (type === ContentType.Text && body && body !== null && typeof body !== "string") {
119
+ body = JSON.stringify(body);
120
+ }
121
+
122
+ return this.instance.request({
123
+ ...requestParams,
124
+ headers: {
125
+ ...(requestParams.headers || {}),
126
+ ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
127
+ },
128
+ params: query,
129
+ responseType: responseFormat,
130
+ data: body,
131
+ url: path,
132
+ <% if (config.unwrapResponseData) { %>
133
+ }).then(response => response.data);
134
+ <% } else { %>
135
+ });
136
+ <% } %>
137
+ };
138
+ }
@@ -0,0 +1,224 @@
1
+ <%
2
+ const { apiConfig, generateResponses, config } = it;
3
+ %>
4
+
5
+ export type QueryParamsType = Record<string | number, any>;
6
+ export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
7
+
8
+ export interface FullRequestParams extends Omit<RequestInit, "body"> {
9
+ /** set parameter to `true` for call `securityWorker` for this request */
10
+ secure?: boolean;
11
+ /** request path */
12
+ path: string;
13
+ /** content type of request body */
14
+ type?: ContentType;
15
+ /** query params */
16
+ query?: QueryParamsType;
17
+ /** format of response (i.e. response.json() -> format: "json") */
18
+ format?: ResponseFormat;
19
+ /** request body */
20
+ body?: unknown;
21
+ /** base url */
22
+ baseUrl?: string;
23
+ /** request cancellation token */
24
+ cancelToken?: CancelToken;
25
+ }
26
+
27
+ export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">
28
+
29
+
30
+ export interface ApiConfig<SecurityDataType = unknown> {
31
+ baseUrl?: string;
32
+ baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
33
+ securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void;
34
+ customFetch?: typeof fetch;
35
+ }
36
+
37
+ export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
38
+ data: D;
39
+ error: E;
40
+ }
41
+
42
+ type CancelToken = Symbol | string | number;
43
+
44
+ export enum ContentType {
45
+ Json = "application/json",
46
+ FormData = "multipart/form-data",
47
+ UrlEncoded = "application/x-www-form-urlencoded",
48
+ Text = "text/plain",
49
+ }
50
+
51
+ export class HttpClient<SecurityDataType = unknown> {
52
+ public baseUrl: string = "<%~ apiConfig.baseUrl %>";
53
+ private securityData: SecurityDataType | null = null;
54
+ private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
55
+ private abortControllers = new Map<CancelToken, AbortController>();
56
+ private customFetch = (...fetchParams: Parameters<typeof fetch>) => fetch(...fetchParams);
57
+
58
+ private baseApiParams: RequestParams = {
59
+ credentials: 'same-origin',
60
+ headers: {},
61
+ redirect: 'follow',
62
+ referrerPolicy: 'no-referrer',
63
+ }
64
+
65
+ constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
66
+ Object.assign(this, apiConfig);
67
+ }
68
+
69
+ public setSecurityData = (data: SecurityDataType | null) => {
70
+ this.securityData = data;
71
+ }
72
+
73
+ protected encodeQueryParam(key: string, value: any) {
74
+ const encodedKey = encodeURIComponent(key);
75
+ return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
76
+ }
77
+
78
+ protected addQueryParam(query: QueryParamsType, key: string) {
79
+ return this.encodeQueryParam(key, query[key]);
80
+ }
81
+
82
+ protected addArrayQueryParam(query: QueryParamsType, key: string) {
83
+ const value = query[key];
84
+ return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
85
+ }
86
+
87
+ protected toQueryString(rawQuery?: QueryParamsType): string {
88
+ const query = rawQuery || {};
89
+ const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
90
+ return keys
91
+ .map((key) =>
92
+ Array.isArray(query[key])
93
+ ? this.addArrayQueryParam(query, key)
94
+ : this.addQueryParam(query, key),
95
+ )
96
+ .join("&");
97
+ }
98
+
99
+ protected addQueryParams(rawQuery?: QueryParamsType): string {
100
+ const queryString = this.toQueryString(rawQuery);
101
+ return queryString ? `?${queryString}` : "";
102
+ }
103
+
104
+ private contentFormatters: Record<ContentType, (input: any) => any> = {
105
+ [ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
106
+ [ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
107
+ [ContentType.FormData]: (input: any) =>
108
+ Object.keys(input || {}).reduce((formData, key) => {
109
+ const property = input[key];
110
+ formData.append(
111
+ key,
112
+ property instanceof Blob ?
113
+ property :
114
+ typeof property === "object" && property !== null ?
115
+ JSON.stringify(property) :
116
+ `${property}`
117
+ );
118
+ return formData;
119
+ }, new FormData()),
120
+ [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
121
+ }
122
+
123
+ protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams {
124
+ return {
125
+ ...this.baseApiParams,
126
+ ...params1,
127
+ ...(params2 || {}),
128
+ headers: {
129
+ ...(this.baseApiParams.headers || {}),
130
+ ...(params1.headers || {}),
131
+ ...((params2 && params2.headers) || {}),
132
+ },
133
+ };
134
+ }
135
+
136
+ protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => {
137
+ if (this.abortControllers.has(cancelToken)) {
138
+ const abortController = this.abortControllers.get(cancelToken);
139
+ if (abortController) {
140
+ return abortController.signal;
141
+ }
142
+ return void 0;
143
+ }
144
+
145
+ const abortController = new AbortController();
146
+ this.abortControllers.set(cancelToken, abortController);
147
+ return abortController.signal;
148
+ }
149
+
150
+ public abortRequest = (cancelToken: CancelToken) => {
151
+ const abortController = this.abortControllers.get(cancelToken)
152
+
153
+ if (abortController) {
154
+ abortController.abort();
155
+ this.abortControllers.delete(cancelToken);
156
+ }
157
+ }
158
+
159
+ public request = async <T = any, E = any>({
160
+ body,
161
+ secure,
162
+ path,
163
+ type,
164
+ query,
165
+ format,
166
+ baseUrl,
167
+ cancelToken,
168
+ ...params
169
+ <% if (config.unwrapResponseData) { %>
170
+ }: FullRequestParams): Promise<T> => {
171
+ <% } else { %>
172
+ }: FullRequestParams): Promise<HttpResponse<T, E>> => {
173
+ <% } %>
174
+ const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {};
175
+ const requestParams = this.mergeRequestParams(params, secureParams);
176
+ const queryString = query && this.toQueryString(query);
177
+ const payloadFormatter = this.contentFormatters[type || ContentType.Json];
178
+ const responseFormat = format || requestParams.format;
179
+
180
+ return this.customFetch(
181
+ `${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`,
182
+ {
183
+ ...requestParams,
184
+ headers: {
185
+ ...(requestParams.headers || {}),
186
+ ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
187
+ },
188
+ signal: cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal,
189
+ body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
190
+ }
191
+ ).then(async (response) => {
192
+ const r = response as HttpResponse<T, E>;
193
+ r.data = (null as unknown) as T;
194
+ r.error = (null as unknown) as E;
195
+
196
+ const data = !responseFormat ? r : await response[responseFormat]()
197
+ .then((data) => {
198
+ if (r.ok) {
199
+ r.data = data;
200
+ } else {
201
+ r.error = data;
202
+ }
203
+ return r;
204
+ })
205
+ .catch((e) => {
206
+ r.error = e;
207
+ return r;
208
+ });
209
+
210
+ if (cancelToken) {
211
+ this.abortControllers.delete(cancelToken);
212
+ }
213
+
214
+ <% if (!config.disableThrowOnError) { %>
215
+ if (!response.ok) throw data;
216
+ <% } %>
217
+ <% if (config.unwrapResponseData) { %>
218
+ return data.data;
219
+ <% } else { %>
220
+ return data;
221
+ <% } %>
222
+ });
223
+ };
224
+ }
@@ -0,0 +1,10 @@
1
+ <%
2
+ const { contract, utils } = it;
3
+ const { formatDescription, require, _ } = utils;
4
+ %>
5
+ export interface <%~ contract.name %> {
6
+ <% _.forEach(contract.$content, (field) => { %>
7
+ <%~ includeFile('@base/object-field-jsdoc.ejs', { ...it, field }) %>
8
+ <%~ field.name %><%~ field.isRequired ? '' : '?' %>: <%~ field.value %><%~ field.isNullable ? ' | null' : ''%>;
9
+ <% }) %>
10
+ }
@@ -0,0 +1,28 @@
1
+ <%
2
+ const { field, utils } = it;
3
+ const { formatDescription, require, _ } = utils;
4
+
5
+ const comments = _.uniq(
6
+ _.compact([
7
+ field.title,
8
+ field.description,
9
+ field.deprecated && ` * @deprecated`,
10
+ !_.isUndefined(field.format) && `@format ${field.format}`,
11
+ !_.isUndefined(field.minimum) && `@min ${field.minimum}`,
12
+ !_.isUndefined(field.maximum) && `@max ${field.maximum}`,
13
+ !_.isUndefined(field.pattern) && `@pattern ${field.pattern}`,
14
+ !_.isUndefined(field.example) &&
15
+ `@example ${_.isObject(field.example) ? JSON.stringify(field.example) : field.example}`,
16
+ ]).reduce((acc, comment) => [...acc, ...comment.split(/\n/g)], []),
17
+ );
18
+ %>
19
+ <% if (comments.length === 1) { %>
20
+ /** <%~ comments[0] %> */
21
+ <% } else if (comments.length) { %>
22
+ /**
23
+ <% comments.forEach(comment => { %>
24
+ * <%~ comment %>
25
+
26
+ <% }) %>
27
+ */
28
+ <% } %>