oip-common 0.0.31 → 0.0.32
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.
- package/fesm2022/oip-common.mjs +120 -109
- package/fesm2022/oip-common.mjs.map +1 -1
- package/index.d.ts +101 -48
- package/package.json +4 -1
- package/scripts/generate-api.mjs +163 -0
- package/templates/api.ejs +3 -3
- package/templates/data-contract-jsdoc.ejs +17 -17
- package/templates/data-contracts.ejs +4 -16
- package/templates/enum-data-contract.ejs +7 -1
- package/templates/http-client.ejs +19 -25
- package/templates/object-field-jsdoc.ejs +8 -8
- package/templates/procedure-call.ejs +5 -2
- package/templates/route-docs.ejs +13 -6
- package/templates/route-type.ejs +2 -1
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
<%
|
|
2
2
|
const { apiConfig, generateResponses, config } = it;
|
|
3
3
|
%>
|
|
4
|
-
|
|
5
|
-
import { SecurityService } from "
|
|
6
|
-
import { LayoutService } from "oip-common";
|
|
4
|
+
import { LayoutService } from "../services/app.layout.service";
|
|
5
|
+
import { SecurityService } from "../services/security.service";
|
|
7
6
|
import { inject, Injectable } from "@angular/core";
|
|
8
7
|
|
|
9
8
|
export type QueryParamsType = Record<string | number, any>;
|
|
@@ -47,6 +46,7 @@ type CancelToken = Symbol | string | number;
|
|
|
47
46
|
|
|
48
47
|
export enum ContentType {
|
|
49
48
|
Json = "application/json",
|
|
49
|
+
JsonApi = "application/vnd.api+json",
|
|
50
50
|
FormData = "multipart/form-data",
|
|
51
51
|
UrlEncoded = "application/x-www-form-urlencoded",
|
|
52
52
|
Text = "text/plain",
|
|
@@ -54,20 +54,9 @@ export enum ContentType {
|
|
|
54
54
|
|
|
55
55
|
@Injectable({ providedIn: 'root' })
|
|
56
56
|
export class HttpClient<SecurityDataType = unknown> {
|
|
57
|
-
protected securityService = inject(SecurityService);
|
|
58
|
-
protected layoutService = inject(LayoutService);
|
|
59
57
|
public baseUrl: string = "<%~ apiConfig.baseUrl %>";
|
|
60
58
|
private securityData: SecurityDataType | null = null;
|
|
61
|
-
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"]
|
|
62
|
-
(securityData) => ({
|
|
63
|
-
headers: {
|
|
64
|
-
"Accept-language": this.layoutService.language()
|
|
65
|
-
? this.layoutService.language()
|
|
66
|
-
: 'en',
|
|
67
|
-
"X-Timezone": this.layoutService.timeZone(),
|
|
68
|
-
Authorization: `Bearer ${securityData}`,
|
|
69
|
-
},
|
|
70
|
-
});
|
|
59
|
+
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
|
|
71
60
|
private abortControllers = new Map<CancelToken, AbortController>();
|
|
72
61
|
private customFetch = (...fetchParams: Parameters<typeof fetch>) => fetch(...fetchParams);
|
|
73
62
|
|
|
@@ -78,10 +67,8 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
78
67
|
referrerPolicy: 'no-referrer',
|
|
79
68
|
}
|
|
80
69
|
|
|
81
|
-
constructor() {
|
|
82
|
-
|
|
83
|
-
this.securityData = token;
|
|
84
|
-
});
|
|
70
|
+
constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
|
|
71
|
+
Object.assign(this, apiConfig);
|
|
85
72
|
}
|
|
86
73
|
|
|
87
74
|
public setSecurityData = (data: SecurityDataType | null) => {
|
|
@@ -121,9 +108,14 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
121
108
|
|
|
122
109
|
private contentFormatters: Record<ContentType, (input: any) => any> = {
|
|
123
110
|
[ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
|
|
111
|
+
[ContentType.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
|
|
124
112
|
[ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
|
|
125
|
-
[ContentType.FormData]: (input: any) =>
|
|
126
|
-
|
|
113
|
+
[ContentType.FormData]: (input: any) => {
|
|
114
|
+
if (input instanceof FormData) {
|
|
115
|
+
return input;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return Object.keys(input || {}).reduce((formData, key) => {
|
|
127
119
|
const property = input[key];
|
|
128
120
|
formData.append(
|
|
129
121
|
key,
|
|
@@ -134,7 +126,8 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
134
126
|
`${property}`
|
|
135
127
|
);
|
|
136
128
|
return formData;
|
|
137
|
-
}, new FormData())
|
|
129
|
+
}, new FormData());
|
|
130
|
+
},
|
|
138
131
|
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
|
|
139
132
|
}
|
|
140
133
|
|
|
@@ -193,7 +186,7 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
193
186
|
const requestParams = this.mergeRequestParams(params, secureParams);
|
|
194
187
|
const queryString = query && this.toQueryString(query);
|
|
195
188
|
const payloadFormatter = this.contentFormatters[type || ContentType.Json];
|
|
196
|
-
|
|
189
|
+
const responseFormat = format || requestParams.format;
|
|
197
190
|
|
|
198
191
|
return this.customFetch(
|
|
199
192
|
`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`,
|
|
@@ -207,14 +200,15 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
207
200
|
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
|
|
208
201
|
}
|
|
209
202
|
).then(async (response) => {
|
|
210
|
-
const r = response
|
|
203
|
+
const r = response as HttpResponse<T, E>;
|
|
211
204
|
r.data = (null as unknown) as T;
|
|
212
205
|
r.error = (null as unknown) as E;
|
|
213
206
|
|
|
214
207
|
if (typeof E !== undefined && responseFormat === undefined)
|
|
215
208
|
responseFormat = 'json';
|
|
216
209
|
|
|
217
|
-
const
|
|
210
|
+
const responseToParse = responseFormat ? response.clone() : response;
|
|
211
|
+
const data = !responseFormat ? r : await responseToParse[responseFormat]()
|
|
218
212
|
.then((data) => {
|
|
219
213
|
if (r.ok) {
|
|
220
214
|
r.data = data;
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
<%
|
|
2
2
|
const { field, utils } = it;
|
|
3
|
-
const { formatDescription, require, _ } = utils;
|
|
3
|
+
const { formatDescription, escapeJSDocContent, require, _ } = utils;
|
|
4
4
|
|
|
5
5
|
const comments = _.uniq(
|
|
6
6
|
_.compact([
|
|
7
|
-
field.title,
|
|
8
|
-
field.description,
|
|
7
|
+
field.title && formatDescription(field.title),
|
|
8
|
+
field.description && formatDescription(field.description),
|
|
9
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}`,
|
|
10
|
+
!_.isUndefined(field.format) && `@format ${escapeJSDocContent(field.format)}`,
|
|
11
|
+
!_.isUndefined(field.minimum) && `@min ${escapeJSDocContent(field.minimum)}`,
|
|
12
|
+
!_.isUndefined(field.maximum) && `@max ${escapeJSDocContent(field.maximum)}`,
|
|
13
|
+
!_.isUndefined(field.pattern) && `@pattern ${escapeJSDocContent(field.pattern)}`,
|
|
14
14
|
!_.isUndefined(field.example) &&
|
|
15
|
-
`@example ${_.isObject(field.example) ? JSON.stringify(field.example) : field.example}`,
|
|
15
|
+
`@example ${escapeJSDocContent(_.isObject(field.example) ? JSON.stringify(field.example) : field.example)}`,
|
|
16
16
|
]).reduce((acc, comment) => [...acc, ...comment.split(/\n/g)], []),
|
|
17
17
|
);
|
|
18
18
|
%>
|
|
@@ -48,6 +48,7 @@ const wrapperArgs = _
|
|
|
48
48
|
// RequestParams["type"]
|
|
49
49
|
const requestContentKind = {
|
|
50
50
|
"JSON": "ContentType.Json",
|
|
51
|
+
"JSON_API": "ContentType.JsonApi",
|
|
51
52
|
"URL_ENCODED": "ContentType.UrlEncoded",
|
|
52
53
|
"FORM_DATA": "ContentType.FormData",
|
|
53
54
|
"TEXT": "ContentType.Text",
|
|
@@ -73,11 +74,13 @@ const describeReturnType = () => {
|
|
|
73
74
|
return `Promise<AxiosResponse<${type}>>`
|
|
74
75
|
}
|
|
75
76
|
default: {
|
|
76
|
-
return `Promise<HttpResponse<${type}, ${errorType}
|
|
77
|
+
return `Promise<HttpResponse<${type}, ${errorType}>>`
|
|
77
78
|
}
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
|
|
82
|
+
const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
83
|
+
|
|
81
84
|
%>
|
|
82
85
|
/**
|
|
83
86
|
<%~ routeDocs.description %>
|
|
@@ -87,7 +90,7 @@ const describeReturnType = () => {
|
|
|
87
90
|
<%~ routeDocs.lines %>
|
|
88
91
|
|
|
89
92
|
*/
|
|
90
|
-
<%~ route.routeName.usage %> = (<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
|
|
93
|
+
<% if (isValidIdentifier(route.routeName.usage)) { %><%~ route.routeName.usage %><% } else { %>"<%~ route.routeName.usage %>"<% } %> = (<%~ wrapperArgs %>)<%~ config.toJS ? `: ${describeReturnType()}` : "" %> =>
|
|
91
94
|
<%~ config.singleHttpClient ? 'this.http.request' : 'this.request' %><<%~ type %>, <%~ errorType %>>({
|
|
92
95
|
path: `<%~ path %>`,
|
|
93
96
|
method: '<%~ _.upperCase(method) %>',
|
package/templates/route-docs.ejs
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
<%
|
|
2
2
|
const { config, route, utils } = it;
|
|
3
|
-
const { _, formatDescription, fmtToJSDocLine, pascalCase, require } = utils;
|
|
3
|
+
const { _, formatDescription, escapeJSDocContent, fmtToJSDocLine, pascalCase, require } = utils;
|
|
4
4
|
const { raw, request, routeName } = route;
|
|
5
5
|
|
|
6
6
|
const jsDocDescription = raw.summary ?
|
|
7
7
|
` * @description ${formatDescription(raw.summary, true)}` :
|
|
8
8
|
fmtToJSDocLine('No description', { eol: false });
|
|
9
9
|
const jsDocLines = _.compact([
|
|
10
|
-
_.size(raw.tags) && ` * @tags ${raw.tags.join(", ")}`,
|
|
11
|
-
` * @name ${_.camelCase(routeName.usage)}`,
|
|
12
|
-
` * @
|
|
10
|
+
_.size(raw.tags) && ` * @tags ${raw.tags.map((tag) => formatDescription(tag, true)).join(", ")}`,
|
|
11
|
+
` * @name ${_.camelCase(escapeJSDocContent(pascalCase(routeName.usage)))}`,
|
|
12
|
+
raw.summary && ` * @summary ${formatDescription(raw.summary, true)}`,
|
|
13
|
+
` * @request ${escapeJSDocContent(_.upperCase(request.method))}:${escapeJSDocContent(raw.route)}`,
|
|
13
14
|
raw.deprecated && ` * @deprecated`,
|
|
14
|
-
routeName.duplicate && ` * @originalName ${routeName.original}`,
|
|
15
|
+
routeName.duplicate && ` * @originalName ${escapeJSDocContent(routeName.original)}`,
|
|
15
16
|
routeName.duplicate && ` * @duplicate`,
|
|
16
17
|
request.security && ` * @secure`,
|
|
17
18
|
...(config.generateResponses && raw.responsesTypes.length
|
|
18
19
|
? raw.responsesTypes.map(
|
|
19
20
|
({ type, status, description, isSuccess }) =>
|
|
20
|
-
` * @response \`${status}\` \`${
|
|
21
|
+
` * @response \`${escapeJSDocContent(status)}\` \`${escapeJSDocContent(type)}\` ${formatDescription(description, true)}`,
|
|
21
22
|
)
|
|
22
23
|
: []),
|
|
24
|
+
...(raw.links?.length
|
|
25
|
+
? raw.links.map(
|
|
26
|
+
({ status, name, operationId, operationRef }) =>
|
|
27
|
+
` * @link \`${status}.${name}\` ${operationId ? `operationId:${operationId}` : `operationRef:${operationRef}`}`,
|
|
28
|
+
)
|
|
29
|
+
: []),
|
|
23
30
|
]).map(str => str.trimEnd()).join("\n");
|
|
24
31
|
|
|
25
32
|
return {
|
package/templates/route-type.ejs
CHANGED
|
@@ -4,6 +4,7 @@ const { _, pascalCase, require } = utils;
|
|
|
4
4
|
const { query, payload, pathParams, headers } = route.request;
|
|
5
5
|
|
|
6
6
|
const routeDocs = includeFile("./route-docs", { config, route, utils });
|
|
7
|
+
const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
7
8
|
const routeNamespace = pascalCase(route.routeName.usage);
|
|
8
9
|
|
|
9
10
|
%>
|
|
@@ -14,7 +15,7 @@ const routeNamespace = pascalCase(route.routeName.usage);
|
|
|
14
15
|
<%~ routeDocs.lines %>
|
|
15
16
|
|
|
16
17
|
*/
|
|
17
|
-
export namespace <%~ routeNamespace %> {
|
|
18
|
+
export namespace <% if (isValidIdentifier(routeNamespace)) { %><%~ routeNamespace %><% } else { %>"<%~ routeNamespace %>"<% } %> {
|
|
18
19
|
export type RequestParams = <%~ (pathParams && pathParams.type) || '{}' %>;
|
|
19
20
|
export type RequestQuery = <%~ (query && query.type) || '{}' %>;
|
|
20
21
|
export type RequestBody = <%~ (payload && payload.type) || 'never' %>;
|