Skip to content

Commit b2a0b34

Browse files
Feat: Support x-scapi-internal (@W-18760261@) (#421)
* refactor * support x-scapi-internal * use RAML toolkit
1 parent ae6ec3e commit b2a0b34

File tree

7 files changed

+355
-318
lines changed

7 files changed

+355
-318
lines changed

package-lock.json

Lines changed: 7 additions & 12 deletions
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
@@ -55,7 +55,7 @@
5555
"tslib": "^2.4.1"
5656
},
5757
"devDependencies": {
58-
"@commerce-apps/raml-toolkit": "file:../raml-toolkit",
58+
"@commerce-apps/raml-toolkit": "^0.7.0",
5959
"@istanbuljs/nyc-config-typescript": "^1.0.2",
6060
"@types/chai": "^4.3.0",
6161
"@types/fs-extra": "^9.0.13",
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
{{! This partial expects to be wrapped in operations/operation }}
2+
{{! and renders the endpoint function for each operation, making the API fetch call }}
3+
/**
4+
* {{{unescapedNotes}}}
5+
*
6+
* If you would like to get a raw Response object use the other {{nickname}} function.
7+
*
8+
* @param options - An object containing the options for this method.
9+
* @param options.parameters - An object containing the parameters for this method.
10+
{{#allParams}}
11+
{{^isBodyParam}}
12+
{{^isHeaderParam}}
13+
{{^isFormParam}}
14+
* @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}}
15+
{{/isFormParam}}
16+
{{/isHeaderParam}}
17+
{{/isBodyParam}}
18+
{{/allParams}}
19+
* @param options.retrySettings - Retry options for the `node-retry` package
20+
* @param options.fetchOptions - Fetch options for the `make-fetch-happen` package
21+
* @param options.headers - An object literal of key value pairs of the headers to be sent with this request.
22+
{{#hasBodyParam}}
23+
* @param options.body - The data to send as the request body.
24+
{{/hasBodyParam}}
25+
{{^hasBodyParam}}
26+
{{#hasFormParams}}
27+
* @param options.body - The data to send as the request body.
28+
{{#formParams}}
29+
{{#isFormParam}}
30+
* @param options.body.{{{paramName}}} - {{{unescapedDescription}}}
31+
{{/isFormParam}}
32+
{{/formParams}}
33+
{{/hasFormParams}}
34+
{{/hasBodyParam}}
35+
*
36+
* @returns A promise of type {{{returnType}}}{{^returnType}}void{{/returnType}}.
37+
*/
38+
{{nickname}}(
39+
options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{
40+
parameters?: CompositeParameters<{
41+
{{#allParams}}
42+
{{^isBodyParam}}
43+
{{^isHeaderParam}}
44+
{{^isFormParam}}
45+
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
46+
{{/isFormParam}}
47+
{{/isHeaderParam}}
48+
{{/isBodyParam}}
49+
{{/allParams}}
50+
} & QueryParameters, CommonParameters>,
51+
retrySettings?: OperationOptions,
52+
fetchOptions?: RequestInit,
53+
headers?: { [key: string]: string },
54+
{{#hasBodyParam}}
55+
{{#bodyParam}}
56+
body: {{{dataType}}}
57+
{{/bodyParam}}
58+
{{/hasBodyParam}}
59+
{{^hasBodyParam}}
60+
{{#hasFormParams}}
61+
body: {{nickname}}BodyType
62+
{{/hasFormParams}}
63+
{{/hasBodyParam}}
64+
}>
65+
): Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}>;
66+
67+
/**
68+
* {{{unescapedNotes}}}
69+
*
70+
* @param options - An object containing the options for this method.
71+
* @param options.parameters - An object containing the parameters for this method.
72+
{{#allParams}}
73+
{{^isBodyParam}}
74+
{{^isHeaderParam}}
75+
{{^isFormParam}}
76+
* @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}}
77+
{{/isFormParam}}
78+
{{/isHeaderParam}}
79+
{{/isBodyParam}}
80+
{{/allParams}}
81+
* @param options.retrySettings - Retry options for the `node-retry` package
82+
* @param options.fetchOptions - Fetch options for the `make-fetch-happen` package
83+
* @param options.headers - An object literal of key value pairs of the headers to be sent with this request.
84+
{{#hasBodyParam}}
85+
* @param options.body - The data to send as the request body.
86+
{{/hasBodyParam}}
87+
{{^hasBodyParam}}
88+
{{#hasFormParams}}
89+
* @param options.body - The data to send as the request body.
90+
{{#formParams}}
91+
{{#isFormParam}}
92+
* @param options.body.{{{paramName}}} - {{{unescapedDescription}}}
93+
{{/isFormParam}}
94+
{{/formParams}}
95+
{{/hasFormParams}}
96+
{{/hasBodyParam}}
97+
* @param rawResponse - Set to true to return entire Response object instead of DTO.
98+
*
99+
* @returns A promise of type Response if rawResponse is true, a promise of type {{{returnType}}}{{^returnType}}void{{/returnType}} otherwise.
100+
*/
101+
{{nickname}}<T extends boolean>(
102+
options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{
103+
parameters?: CompositeParameters<{
104+
{{#allParams}}
105+
{{^isBodyParam}}
106+
{{^isHeaderParam}}
107+
{{^isFormParam}}
108+
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
109+
{{/isFormParam}}
110+
{{/isHeaderParam}}
111+
{{/isBodyParam}}
112+
{{/allParams}}
113+
} & QueryParameters, CommonParameters>,
114+
retrySettings?: OperationOptions,
115+
fetchOptions?: RequestInit,
116+
headers?: { [key: string]: string },
117+
{{#hasBodyParam}}
118+
{{#bodyParam}}
119+
body: {{{dataType}}}
120+
{{/bodyParam}}
121+
{{/hasBodyParam}}
122+
{{^hasBodyParam}}
123+
{{#hasFormParams}}
124+
body: {{nickname}}BodyType
125+
{{/hasFormParams}}
126+
{{/hasBodyParam}}
127+
}>,
128+
rawResponse?: T
129+
): Promise<T extends true ? Response : {{{returnType}}}{{^returnType}}void{{/returnType}}>;
130+
131+
/**
132+
* {{{unescapedNotes}}}
133+
*
134+
* @param options - An object containing the options for this method.
135+
* @param options.parameters - An object containing the parameters for this method.
136+
{{#allParams}}
137+
{{^isBodyParam}}
138+
{{^isHeaderParam}}
139+
{{^isFormParam}}
140+
* @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}}
141+
{{/isFormParam}}
142+
{{/isHeaderParam}}
143+
{{/isBodyParam}}
144+
{{/allParams}}
145+
* @param options.retrySettings - Retry options for the `node-retry` package
146+
* @param options.fetchOptions - Fetch options for the `make-fetch-happen` package
147+
* @param options.headers - An object literal of key value pairs of the headers to be sent with this request.
148+
{{#hasBodyParam}}
149+
* @param options.body - The data to send as the request body.
150+
{{/hasBodyParam}}
151+
{{^hasBodyParam}}
152+
{{#hasFormParams}}
153+
* @param options.body - The data to send as the request body.
154+
{{#formParams}}
155+
{{#isFormParam}}
156+
* @param options.body.{{{paramName}}} - {{{unescapedDescription}}}
157+
{{/isFormParam}}
158+
{{/formParams}}
159+
{{/hasFormParams}}
160+
{{/hasBodyParam}}
161+
* @param rawResponse - Set to true to return entire Response object instead of DTO.
162+
*
163+
* @returns A promise of type Response if rawResponse is true, a promise of type {{{returnType}}}{{^returnType}}void{{/returnType}} otherwise.
164+
*/
165+
async {{nickname}}(
166+
options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{
167+
parameters?: CompositeParameters<{
168+
{{#allParams}}
169+
{{^isBodyParam}}
170+
{{^isHeaderParam}}
171+
{{^isFormParam}}
172+
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
173+
{{/isFormParam}}
174+
{{/isHeaderParam}}
175+
{{/isBodyParam}}
176+
{{/allParams}}
177+
} & QueryParameters, CommonParameters>,
178+
retrySettings?: OperationOptions,
179+
fetchOptions?: RequestInit,
180+
headers?: { [key: string]: string },
181+
{{#hasBodyParam}}
182+
{{#bodyParam}}
183+
body: {{{dataType}}}
184+
{{/bodyParam}}
185+
{{/hasBodyParam}}
186+
{{^hasBodyParam}}
187+
{{#hasFormParams}}
188+
body: {{nickname}}BodyType
189+
{{/hasFormParams}}
190+
{{/hasBodyParam}}
191+
}>,
192+
rawResponse?: boolean
193+
): Promise<Response | {{{returnType}}}{{^returnType}}void{{/returnType}}> {
194+
const optionParams = options?.parameters || ({} as Partial<NonNullable<NonNullable<typeof options>["parameters"]>>);
195+
const configParams = this.clientConfig.parameters as CommonParameters;
196+
197+
const pathParams = {};
198+
199+
{{#pathParams}}
200+
if (optionParams["{{paramName}}"] !== undefined) {
201+
pathParams["{{paramName}}"] = optionParams["{{paramName}}"];
202+
} else if (configParams["{{paramName}}"] !== undefined) {
203+
pathParams["{{paramName}}"] = configParams["{{paramName}}"];
204+
}
205+
{{#required}}
206+
else {
207+
throw new Error('Missing required path parameter: {{paramName}}');
208+
}
209+
{{/required}}
210+
{{/pathParams}}
211+
212+
const queryParams: QueryParameters = {};
213+
214+
{{#queryParams}}
215+
if (optionParams["{{paramName}}"] !== undefined) {
216+
queryParams["{{paramName}}"] = optionParams["{{paramName}}"];
217+
} else if (configParams["{{paramName}}"] !== undefined) {
218+
queryParams["{{paramName}}"] = configParams["{{paramName}}"];
219+
}
220+
{{#required}}
221+
else {
222+
throw new Error('Missing required query parameter: {{paramName}}');
223+
}
224+
{{/required}}
225+
{{/queryParams}}
226+
227+
Object.keys(optionParams).forEach((key) => {
228+
const paramValue = optionParams[key as keyof typeof optionParams];
229+
if(paramValue !== undefined && (key.startsWith('c_') || !((key in queryParams) || (key in pathParams)))) {
230+
if(!key.startsWith('c_')) {
231+
console.warn(`Found unknown parameter for {{{nickname}}}: ${key}, adding as query parameter anyway`);
232+
}
233+
queryParams[key as keyof typeof queryParams] = paramValue;
234+
}
235+
})
236+
237+
const headers = mergeHeaders(getHeaders(options), {
238+
{{#hasConsumes}}
239+
{{#consumes}}
240+
"Content-Type": "{{mediaType}}",
241+
{{/consumes}}
242+
{{/hasConsumes}}
243+
[USER_AGENT_HEADER]: USER_AGENT_VALUE
244+
});
245+
246+
return StaticClient.{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}({
247+
client: this,
248+
rawResponse: rawResponse,
249+
path: "{{path}}",
250+
pathParameters: pathParams,
251+
queryParameters: queryParams,
252+
retrySettings: (options || {}).retrySettings,
253+
headers: headers,
254+
{{#hasBodyParam}}
255+
body: (options || {}).body,
256+
{{/hasBodyParam}}
257+
{{^hasBodyParam}}
258+
{{#hasFormParams}}
259+
body: (options || {}).body,
260+
{{/hasFormParams}}
261+
{{/hasBodyParam}}
262+
...(options{{^hasBodyParam}}?{{/hasBodyParam}}.fetchOptions && { fetchOptions: options.fetchOptions })
263+
});
264+
}

templatesOas/apis.enum.mustache

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{! This partial expects to be wrapped in operations/operation }}
2+
{{! and renders the enum types for a given operation }}
3+
{{#allParams}}
4+
{{#isEnum}}
5+
{{#stringEnums}}
6+
export enum {{operationIdCamelCase}}{{enumName}} {
7+
{{#allowableValues}}
8+
{{#enumVars}}
9+
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
10+
{{/enumVars}}
11+
{{/allowableValues}}
12+
}
13+
{{/stringEnums}}
14+
{{^stringEnums}}
15+
16+
export const {{operationIdCamelCase}}{{enumName}} = {
17+
{{#allowableValues}}
18+
{{#enumVars}}
19+
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
20+
{{/enumVars}}
21+
{{/allowableValues}}
22+
} as const;
23+
export type {{operationIdCamelCase}}{{enumName}} = typeof {{operationIdCamelCase}}{{enumName}}[keyof typeof {{operationIdCamelCase}}{{enumName}}];
24+
{{/stringEnums}}
25+
{{/isEnum}}
26+
{{/allParams}}

0 commit comments

Comments
 (0)