diff --git a/templatesOas/apis.endpoint.mustache b/templatesOas/apis.endpoint.mustache new file mode 100644 index 00000000..303adc44 --- /dev/null +++ b/templatesOas/apis.endpoint.mustache @@ -0,0 +1,279 @@ + +{{! This partial expects to be wrapped in operations/operation }} +{{! and renders the endpoint function for each operation, making the API fetch call }} + /** + * {{{unescapedNotes}}} + * + * If you would like to get a raw Response object use the other {{nickname}} function. + * + * @param options - An object containing the options for this method. + * @param options.parameters - An object containing the parameters for this method. + {{#allParams}} + {{^isBodyParam}} + {{^isHeaderParam}} + {{^isFormParam}} + * @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}} + {{/isFormParam}} + {{/isHeaderParam}} + {{/isBodyParam}} + {{/allParams}} + * @param options.headers - An object literal of key value pairs of the headers to be sent with this request. + {{#hasBodyParam}} + * @param options.body - The data to send as the request body. + {{/hasBodyParam}} + {{^hasBodyParam}} + {{#hasFormParams}} + * @param options.body - The data to send as the request body. + {{#formParams}} + {{#isFormParam}} + * @param options.body.{{{paramName}}} - {{{unescapedDescription}}} + {{/isFormParam}} + {{/formParams}} + {{/hasFormParams}} + {{/hasBodyParam}} + * + * @returns A promise of type {{{returnType}}}{{^returnType}}void{{/returnType}}. + */ + {{nickname}}( + options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{ + parameters?: CompositeParameters<{ + {{#allParams}} + {{^isBodyParam}} + {{^isHeaderParam}} + {{^isFormParam}} + {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; + {{/isFormParam}} + {{/isHeaderParam}} + {{/isBodyParam}} + {{/allParams}} + } & QueryParameters, ConfigParameters> + headers?: { [key: string]: string }, + {{#hasBodyParam}} + {{#bodyParam}} + body: {{{dataType}}} & CustomRequestBody + {{/bodyParam}} + {{/hasBodyParam}} + {{^hasBodyParam}} + {{#hasFormParams}} + body: {{nickname}}BodyType & CustomRequestBody + {{/hasFormParams}} + {{/hasBodyParam}} + }> + ): Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}>; + + /** + * {{{unescapedNotes}}} + * + * @param options - An object containing the options for this method. + * @param options.parameters - An object containing the parameters for this method. + {{#allParams}} + {{^isBodyParam}} + {{^isHeaderParam}} + {{^isFormParam}} + * @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}} + {{/isFormParam}} + {{/isHeaderParam}} + {{/isBodyParam}} + {{/allParams}} + * @param options.headers - An object literal of key value pairs of the headers to be sent with this request. + {{#hasBodyParam}} + * @param options.body - The data to send as the request body. + {{/hasBodyParam}} + {{^hasBodyParam}} + {{#hasFormParams}} + * @param options.body - The data to send as the request body. + {{#formParams}} + {{#isFormParam}} + * @param options.body.{{{paramName}}} - {{{unescapedDescription}}} + {{/isFormParam}} + {{/formParams}} + {{/hasFormParams}} + {{/hasBodyParam}} + * @param rawResponse - Set to true to return entire Response object instead of DTO. + * + * @returns A promise of type Response if rawResponse is true, a promise of type {{{returnType}}}{{^returnType}}void{{/returnType}} otherwise. + */ + {{nickname}}( + options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{ + parameters?: CompositeParameters<{ + {{#allParams}} + {{^isBodyParam}} + {{^isHeaderParam}} + {{^isFormParam}} + {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; + {{/isFormParam}} + {{/isHeaderParam}} + {{/isBodyParam}} + {{/allParams}} + } & QueryParameters, ConfigParameters> + headers?: { [key: string]: string }, + {{#hasBodyParam}} + {{#bodyParam}} + body: {{{dataType}}} & CustomRequestBody + {{/bodyParam}} + {{/hasBodyParam}} + {{^hasBodyParam}} + {{#hasFormParams}} + body: {{nickname}}BodyType & CustomRequestBody + {{/hasFormParams}} + {{/hasBodyParam}} + }>, + rawResponse?: T + ): Promise; + + /** + * {{{unescapedNotes}}} + * + * @param options - An object containing the options for this method. + * @param options.parameters - An object containing the parameters for this method. + {{#allParams}} + {{^isBodyParam}} + {{^isHeaderParam}} + {{^isFormParam}} + * @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}} + {{/isFormParam}} + {{/isHeaderParam}} + {{/isBodyParam}} + {{/allParams}} + * @param options.headers - An object literal of key value pairs of the headers to be sent with this request. + {{#hasBodyParam}} + * @param options.body - The data to send as the request body. + {{/hasBodyParam}} + {{^hasBodyParam}} + {{#hasFormParams}} + * @param options.body - The data to send as the request body. + {{#formParams}} + {{#isFormParam}} + * @param options.body.{{{paramName}}} - {{{unescapedDescription}}} + {{/isFormParam}} + {{/formParams}} + {{/hasFormParams}} + {{/hasBodyParam}} + * @param rawResponse - Set to true to return entire Response object instead of DTO. + * + * @returns A promise of type Response if rawResponse is true, a promise of type {{{returnType}}}{{^returnType}}void{{/returnType}} otherwise. + */ + async {{nickname}}( + options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{ + parameters?: CompositeParameters<{ + {{#allParams}} + {{^isBodyParam}} + {{^isHeaderParam}} + {{^isFormParam}} + {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; + {{/isFormParam}} + {{/isHeaderParam}} + {{/isBodyParam}} + {{/allParams}} + } & QueryParameters, ConfigParameters>, + headers?: { [key: string]: string }, + {{#hasBodyParam}} + {{#bodyParam}} + body: {{{dataType}}} & CustomRequestBody + {{/bodyParam}} + {{/hasBodyParam}} + {{^hasBodyParam}} + {{#hasFormParams}} + body: {{nickname}}BodyType & CustomRequestBody + {{/hasFormParams}} + {{/hasBodyParam}} + }>, + rawResponse?: boolean + ): Promise { + const optionParams = options?.parameters || ({} as Partial["parameters"]>>); + const configParams = this.clientConfig.parameters; + + const pathParams: {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}PathParameters & Required = { + shortCode: configParams.shortCode, + }; + + {{#pathParams}} + if (optionParams["{{paramName}}"] !== undefined) { + pathParams["{{paramName}}"] = optionParams["{{paramName}}"]; + } else if (configParams["{{paramName}}"] !== undefined) { + pathParams["{{paramName}}"] = configParams["{{paramName}}"]; + } + {{#required}} + else { + throw new Error('Missing required path parameter: {{paramName}}'); + } + {{/required}} + {{/pathParams}} + + const queryParams: Partial<{{nickname}}QueryParameters> & QueryParameters = {}; + + {{#queryParams}} + if (optionParams["{{paramName}}"] !== undefined) { + queryParams["{{paramName}}"] = optionParams["{{paramName}}"]; + } else if (configParams["{{paramName}}"] !== undefined) { + queryParams["{{paramName}}"] = configParams["{{paramName}}"]; + } + {{#required}} + else { + throw new Error('Missing required query parameter: {{paramName}}'); + } + {{/required}} + {{/queryParams}} + + Object.keys(optionParams).forEach((key) => { + const paramValue = optionParams[key as keyof typeof optionParams]; + if(paramValue !== undefined && (key.startsWith('c_') || !((key in queryParams) || (key in pathParams)))) { + if(!key.startsWith('c_')) { + console.warn(`Found unknown parameter for {{{nickname}}}: ${key}, adding as query parameter anyway`); + } + queryParams[key as keyof typeof queryParams] = paramValue; + } + }) + + const url = new TemplateURL( + "{{{path}}}", + this.clientConfig.baseUri, + { + pathParams, + queryParams, + origin: this.clientConfig.proxy + } + ); + + const headers: Record = { + {{#hasConsumes}} + {{#consumes}} + "Content-Type": "{{mediaType}}", + {{/consumes}} + {{/hasConsumes}} + ...this.clientConfig.headers, + ...options?.headers + }; + + if (!isBrowser) { + // Browsers forbid setting a custom user-agent header + headers[USER_AGENT_HEADER] = [headers[USER_AGENT_HEADER], USER_AGENT_VALUE].join(" "); + } + + const response = await doFetch( + url.toString(), + { + method: '{{httpMethod}}', + headers, + {{#hasBodyParam}} + body: this.clientConfig.transformRequest(options{{^hasBodyParam}}?{{/hasBodyParam}}.body, headers) + {{/hasBodyParam}} + {{^hasBodyParam}} + {{#hasFormParams}} + body: this.clientConfig.transformRequest(options{{^hasBodyParam}}?{{/hasBodyParam}}.body, headers) + {{/hasFormParams}} + {{/hasBodyParam}} + }, + this.clientConfig, + rawResponse + ) + + {{^returnType}} + if (rawResponse) { + return response as Response; + } + {{/returnType}} + {{#returnType}} + return response as Response | {{{returnType}}}; + {{/returnType}} + } \ No newline at end of file diff --git a/templatesOas/apis.enum.mustache b/templatesOas/apis.enum.mustache new file mode 100644 index 00000000..641e44dc --- /dev/null +++ b/templatesOas/apis.enum.mustache @@ -0,0 +1,25 @@ +{{! This partial expects to be wrapped in operations/operation }} +{{! and renders the enum types for a given operation }} +{{#allParams}} +{{#isEnum}} +{{#stringEnums}} +export enum {{operationIdCamelCase}}{{enumName}} { +{{#allowableValues}} + {{#enumVars}} + {{{name}}} = {{{value}}}{{^-last}},{{/-last}} + {{/enumVars}} +{{/allowableValues}} +} + +{{/stringEnums}} +{{^stringEnums}} +export type {{operationIdCamelCase}}{{enumName}} = +{{#allowableValues}} + {{#enumVars}} + {{{value}}}{{^-last}} |{{/-last}}{{#-last}};{{/-last}} + {{/enumVars}} +{{/allowableValues}} + +{{/stringEnums}} +{{/isEnum}} +{{/allParams}} \ No newline at end of file diff --git a/templatesOas/apis.mustache b/templatesOas/apis.mustache index be9a3493..3e4baa93 100644 --- a/templatesOas/apis.mustache +++ b/templatesOas/apis.mustache @@ -22,58 +22,46 @@ import type { {{#hasEnums}} {{#operations}} {{#operation}} -{{#allParams}} -{{#isEnum}} -{{#stringEnums}} -export enum {{operationIdCamelCase}}{{enumName}} { -{{#allowableValues}} - {{#enumVars}} - {{{name}}} = {{{value}}}{{^-last}},{{/-last}} - {{/enumVars}} -{{/allowableValues}} -} -{{/stringEnums}} -{{^stringEnums}} - -export type {{operationIdCamelCase}}{{enumName}} = -{{#allowableValues}} - {{#enumVars}} - {{{value}}}{{^-last}} |{{/-last}}{{#-last}};{{/-last}} - {{/enumVars}} -{{/allowableValues}} -{{/stringEnums}} -{{/isEnum}} -{{/allParams}} +{{#vendorExtensions}} +{{^x-scapi-internal}} +{{> apis.enum }} +{{/x-scapi-internal}} +{{/vendorExtensions}} +{{^vendorExtensions}} +{{> apis.enum }} +{{/vendorExtensions}} {{/operation}} {{/operations}} {{/hasEnums}} - {{#operations}} {{#operation}} -export type {{nickname}}QueryParameters = { - {{#queryParams}} - {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{dataType}}}{{/isEnum}}{{^isEnum}}{{#hasReadOnly}}Omit<{{{dataType}}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{{dataType}}}{{/hasReadOnly}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; - {{/queryParams}} -} - -export type {{nickname}}PathParameters = { - {{#pathParams}} - {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{#hasReadOnly}}Omit<{{{dataType}}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{{dataType}}}{{/hasReadOnly}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; - {{/pathParams}} -} - +{{#vendorExtensions}} +{{^x-scapi-internal}} +{{> apis.parameters }} +{{/x-scapi-internal}} +{{/vendorExtensions}} +{{^vendorExtensions}} +{{> apis.parameters }} +{{/vendorExtensions}} {{/operation}} {{/operations}} - /** * All path parameters that are used by at least one {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}} method. */ export type {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}PathParameters = Partial< {{#operations}} {{#operation}} - {{nickname}}PathParameters{{^-last}} &{{/-last}} + {{#vendorExtensions}} + {{^x-scapi-internal}} + {{nickname}}PathParameters & + {{/x-scapi-internal}} + {{/vendorExtensions}} + {{^vendorExtensions}} + {{nickname}}PathParameters & + {{/vendorExtensions}} {{/operation}} {{/operations}} + {} {{! We have this to avoid trailing a `&`, due to x-scapi-internal we can't use the `last` keyword}} >; /** @@ -82,9 +70,17 @@ export type {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camel export type {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}QueryParameters = Partial< {{#operations}} {{#operation}} - {{nickname}}QueryParameters{{^-last}} &{{/-last}} + {{#vendorExtensions}} + {{^x-scapi-internal}} + {{nickname}}QueryParameters & + {{/x-scapi-internal}} + {{/vendorExtensions}} + {{^vendorExtensions}} + {{nickname}}QueryParameters & + {{/vendorExtensions}} {{/operation}} {{/operations}} + {} {{! We have this to avoid trailing a `&`, due to x-scapi-internal we can't use the `last` keyword}} >; /** @@ -92,20 +88,6 @@ export type {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camel */ export type {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}Parameters = {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}PathParameters & BaseUriParameters & {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}QueryParameters; -{{#operations}} -{{#operation}} -{{#hasFormParams}} -export type {{nickname}}BodyType = { - {{#formParams}} - {{#isFormParam}} - {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{dataType}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; - {{/isFormParam}} - {{/formParams}} -} - -{{/hasFormParams}} -{{/operation}} -{{/operations}} /** * [{{appName}}](https://developer.salesforce.com/docs/commerce/commerce-api/references?meta={{#lambda.kebabcase}}{{appName}}{{/lambda.kebabcase}}:Summary) * ================================== @@ -144,7 +126,14 @@ export class {{#vendorExtensions}}{{#x-sdk-classname}}{{{ . }}}{{/x-sdk-classnam static readonly apiPaths = { {{#operations}} {{#operation}} + {{#vendorExtensions}} + {{^x-scapi-internal}} + {{nickname}}: "{{path}}", + {{/x-scapi-internal}} + {{/vendorExtensions}} + {{^vendorExtensions}} {{nickname}}: "{{path}}", + {{/vendorExtensions}} {{/operation}} {{/operations}} } @@ -159,6 +148,8 @@ export class {{#vendorExtensions}}{{#x-sdk-classname}}{{{ . }}}{{/x-sdk-classnam static readonly paramKeys = { {{#operations}} {{#operation}} + {{#vendorExtensions}} + {{^x-scapi-internal}} {{nickname}}: [ {{#allParams}} {{^isBodyParam}} @@ -179,289 +170,44 @@ export class {{#vendorExtensions}}{{#x-sdk-classname}}{{{ . }}}{{/x-sdk-classnam {{/isBodyParam}} {{/allParams}} ], - {{/operation}} - {{/operations}} - } as const; - -{{#operations}} -{{#operation}} - - /** - * {{{unescapedNotes}}} - * - * If you would like to get a raw Response object use the other {{nickname}} function. - * - * @param options - An object containing the options for this method. - * @param options.parameters - An object containing the parameters for this method. - {{#allParams}} - {{^isBodyParam}} - {{^isHeaderParam}} - {{^isFormParam}} - * @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}} - {{/isFormParam}} - {{/isHeaderParam}} - {{/isBodyParam}} - {{/allParams}} - * @param options.headers - An object literal of key value pairs of the headers to be sent with this request. - {{#hasBodyParam}} - * @param options.body - The data to send as the request body. - {{/hasBodyParam}} - {{^hasBodyParam}} - {{#hasFormParams}} - * @param options.body - The data to send as the request body. - {{#formParams}} - {{#isFormParam}} - * @param options.body.{{{paramName}}} - {{{unescapedDescription}}} - {{/isFormParam}} - {{/formParams}} - {{/hasFormParams}} - {{/hasBodyParam}} - * - * @returns A promise of type {{{returnType}}}{{^returnType}}void{{/returnType}}. - */ - {{nickname}}( - options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{ - parameters?: CompositeParameters<{ + {{/x-scapi-internal}} + {{/vendorExtensions}} + {{^vendorExtensions}} + {{nickname}}: [ {{#allParams}} {{^isBodyParam}} {{^isHeaderParam}} - {{^isFormParam}} - {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; - {{/isFormParam}} + '{{paramName}}', {{/isHeaderParam}} {{/isBodyParam}} {{/allParams}} - } & QueryParameters, ConfigParameters> - headers?: { [key: string]: string }, - {{#hasBodyParam}} - {{#bodyParam}} - body: {{{dataType}}} & CustomRequestBody - {{/bodyParam}} - {{/hasBodyParam}} - {{^hasBodyParam}} - {{#hasFormParams}} - body: {{nickname}}BodyType & CustomRequestBody - {{/hasFormParams}} - {{/hasBodyParam}} - }> - ): Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}>; - - /** - * {{{unescapedNotes}}} - * - * @param options - An object containing the options for this method. - * @param options.parameters - An object containing the parameters for this method. - {{#allParams}} - {{^isBodyParam}} - {{^isHeaderParam}} - {{^isFormParam}} - * @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}} - {{/isFormParam}} - {{/isHeaderParam}} - {{/isBodyParam}} - {{/allParams}} - * @param options.headers - An object literal of key value pairs of the headers to be sent with this request. - {{#hasBodyParam}} - * @param options.body - The data to send as the request body. - {{/hasBodyParam}} - {{^hasBodyParam}} - {{#hasFormParams}} - * @param options.body - The data to send as the request body. - {{#formParams}} - {{#isFormParam}} - * @param options.body.{{{paramName}}} - {{{unescapedDescription}}} - {{/isFormParam}} - {{/formParams}} - {{/hasFormParams}} - {{/hasBodyParam}} - * @param rawResponse - Set to true to return entire Response object instead of DTO. - * - * @returns A promise of type Response if rawResponse is true, a promise of type {{{returnType}}}{{^returnType}}void{{/returnType}} otherwise. - */ - {{nickname}}( - options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{ - parameters?: CompositeParameters<{ - {{#allParams}} - {{^isBodyParam}} - {{^isHeaderParam}} - {{^isFormParam}} - {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; - {{/isFormParam}} - {{/isHeaderParam}} - {{/isBodyParam}} - {{/allParams}} - } & QueryParameters, ConfigParameters> - headers?: { [key: string]: string }, - {{#hasBodyParam}} - {{#bodyParam}} - body: {{{dataType}}} & CustomRequestBody - {{/bodyParam}} - {{/hasBodyParam}} - {{^hasBodyParam}} - {{#hasFormParams}} - body: {{nickname}}BodyType & CustomRequestBody - {{/hasFormParams}} - {{/hasBodyParam}} - }>, - rawResponse?: T - ): Promise; - - /** - * {{{unescapedNotes}}} - * - * @param options - An object containing the options for this method. - * @param options.parameters - An object containing the parameters for this method. - {{#allParams}} - {{^isBodyParam}} - {{^isHeaderParam}} - {{^isFormParam}} - * @param options.parameters.{{{paramName}}} - {{{unescapedDescription}}} - {{/isFormParam}} - {{/isHeaderParam}} - {{/isBodyParam}} - {{/allParams}} - * @param options.headers - An object literal of key value pairs of the headers to be sent with this request. - {{#hasBodyParam}} - * @param options.body - The data to send as the request body. - {{/hasBodyParam}} - {{^hasBodyParam}} - {{#hasFormParams}} - * @param options.body - The data to send as the request body. - {{#formParams}} - {{#isFormParam}} - * @param options.body.{{{paramName}}} - {{{unescapedDescription}}} - {{/isFormParam}} - {{/formParams}} - {{/hasFormParams}} - {{/hasBodyParam}} - * @param rawResponse - Set to true to return entire Response object instead of DTO. - * - * @returns A promise of type Response if rawResponse is true, a promise of type {{{returnType}}}{{^returnType}}void{{/returnType}} otherwise. - */ - async {{nickname}}( - options{{^hasBodyParam}}?{{/hasBodyParam}}: RequireParametersUnlessAllAreOptional<{ - parameters?: CompositeParameters<{ + ], + {{nickname}}Required: [ {{#allParams}} - {{^isBodyParam}} - {{^isHeaderParam}} - {{^isFormParam}} - {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; - {{/isFormParam}} - {{/isHeaderParam}} - {{/isBodyParam}} + {{^isBodyParam}} + {{^isHeaderParam}} + {{#required}} + '{{paramName}}', + {{/required}} + {{/isHeaderParam}} + {{/isBodyParam}} {{/allParams}} - } & QueryParameters, ConfigParameters>, - headers?: { [key: string]: string }, - {{#hasBodyParam}} - {{#bodyParam}} - body: {{{dataType}}} & CustomRequestBody - {{/bodyParam}} - {{/hasBodyParam}} - {{^hasBodyParam}} - {{#hasFormParams}} - body: {{nickname}}BodyType & CustomRequestBody - {{/hasFormParams}} - {{/hasBodyParam}} - }>, - rawResponse?: boolean - ): Promise { - const optionParams = options?.parameters || ({} as Partial["parameters"]>>); - const configParams = this.clientConfig.parameters; - - const pathParams: {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}PathParameters & Required = { - shortCode: configParams.shortCode, - }; - - {{#pathParams}} - if (optionParams["{{paramName}}"] !== undefined) { - pathParams["{{paramName}}"] = optionParams["{{paramName}}"]; - } else if (configParams["{{paramName}}"] !== undefined) { - pathParams["{{paramName}}"] = configParams["{{paramName}}"]; - } - {{#required}} - else { - throw new Error('Missing required path parameter: {{paramName}}'); - } - {{/required}} - {{/pathParams}} - - const queryParams: Partial<{{nickname}}QueryParameters> & QueryParameters = {}; - - {{#queryParams}} - if (optionParams["{{paramName}}"] !== undefined) { - queryParams["{{paramName}}"] = optionParams["{{paramName}}"]; - } else if (configParams["{{paramName}}"] !== undefined) { - queryParams["{{paramName}}"] = configParams["{{paramName}}"]; - } - {{#required}} - else { - throw new Error('Missing required query parameter: {{paramName}}'); - } - {{/required}} - {{/queryParams}} - - Object.keys(optionParams).forEach((key) => { - const paramValue = optionParams[key as keyof typeof optionParams]; - if(paramValue !== undefined && (key.startsWith('c_') || !((key in queryParams) || (key in pathParams)))) { - if(!key.startsWith('c_')) { - console.warn(`Found unknown parameter for {{{nickname}}}: ${key}, adding as query parameter anyway`); - } - queryParams[key as keyof typeof queryParams] = paramValue; - } - }) - - const url = new TemplateURL( - "{{{path}}}", - this.clientConfig.baseUri, - { - pathParams, - queryParams, - origin: this.clientConfig.proxy - } - ); - - const headers: Record = { - {{#hasConsumes}} - {{#consumes}} - "Content-Type": "{{mediaType}}", - {{/consumes}} - {{/hasConsumes}} - ...this.clientConfig.headers, - ...options?.headers - }; - - if (!isBrowser) { - // Browsers forbid setting a custom user-agent header - headers[USER_AGENT_HEADER] = [headers[USER_AGENT_HEADER], USER_AGENT_VALUE].join(" "); - } - - const response = await doFetch( - url.toString(), - { - method: '{{httpMethod}}', - headers, - {{#hasBodyParam}} - body: this.clientConfig.transformRequest(options{{^hasBodyParam}}?{{/hasBodyParam}}.body, headers) - {{/hasBodyParam}} - {{^hasBodyParam}} - {{#hasFormParams}} - body: this.clientConfig.transformRequest(options{{^hasBodyParam}}?{{/hasBodyParam}}.body, headers) - {{/hasFormParams}} - {{/hasBodyParam}} - }, - this.clientConfig, - rawResponse - ) + ], + {{/vendorExtensions}} + {{/operation}} + {{/operations}} + } as const; - {{^returnType}} - if (rawResponse) { - return response as Response; - } - {{/returnType}} - {{#returnType}} - return response as Response | {{{returnType}}}; - {{/returnType}} - } +{{#operations}} +{{#operation}} +{{#vendorExtensions}} +{{^x-scapi-internal}} +{{> apis.endpoint }} +{{/x-scapi-internal}} +{{/vendorExtensions}} +{{^vendorExtensions}} +{{> apis.endpoint }} +{{/vendorExtensions}} {{/operation}} {{/operations}} } diff --git a/templatesOas/apis.parameters.mustache b/templatesOas/apis.parameters.mustache new file mode 100644 index 00000000..cbf4c703 --- /dev/null +++ b/templatesOas/apis.parameters.mustache @@ -0,0 +1,23 @@ +{{! This partial expects to be wrapped in operations/operation }} +{{! and renders the parameter types for a given operation - query, path, and form body parameters }} +export type {{nickname}}QueryParameters = { + {{#queryParams}} + {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{dataType}}}{{/isEnum}}{{^isEnum}}{{#hasReadOnly}}Omit<{{{dataType}}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{{dataType}}}{{/hasReadOnly}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; + {{/queryParams}} +} + +export type {{nickname}}PathParameters = { + {{#pathParams}} + {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{#hasReadOnly}}Omit<{{{dataType}}}, {{#readOnlyVars}}'{{baseName}}'{{^-last}}|{{/-last}}{{/readOnlyVars}}>{{/hasReadOnly}}{{^hasReadOnly}}{{{dataType}}}{{/hasReadOnly}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; + {{/pathParams}} +} +{{#hasFormParams}} + +export type {{nickname}}BodyType = { + {{#formParams}} + {{#isFormParam}} + {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{dataType}}}{{/isEnum}}{{^isEnum}}{{^isDateTime}}{{^isDate}}{{{dataType}}}{{/isDate}}{{/isDateTime}}{{#isDateTime}}string{{/isDateTime}}{{#isDate}}string{{/isDate}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; + {{/isFormParam}} + {{/formParams}} +} +{{/hasFormParams}} \ No newline at end of file diff --git a/templatesOas/index.mustache b/templatesOas/index.mustache index e2b23276..2e6f596f 100644 --- a/templatesOas/index.mustache +++ b/templatesOas/index.mustache @@ -19,6 +19,8 @@ export namespace {{#apiInfo}}{{#apis.0}}{{#vendorExtensions}}{{#x-sdk-classname} {{#apis}} {{#operations}} {{#operation}} + {{#vendorExtensions}} + {{^x-scapi-internal}} {{#allParams}} {{#isEnum}} {{^stringEnums}} @@ -31,6 +33,22 @@ export namespace {{#apiInfo}}{{#apis.0}}{{#vendorExtensions}}{{#x-sdk-classname} {{/hasFormParams}} export type {{nickname}}QueryParameters = {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}ApiTypes.{{nickname}}QueryParameters export type {{nickname}}PathParameters = {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}ApiTypes.{{nickname}}PathParameters + {{/x-scapi-internal}} + {{/vendorExtensions}} + {{^vendorExtensions}} + {{#allParams}} + {{#isEnum}} + {{^stringEnums}} + export type {{operationIdCamelCase}}{{enumName}} = {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}ApiTypes.{{operationIdCamelCase}}{{enumName}} + {{/stringEnums}} + {{/isEnum}} + {{/allParams}} + {{#hasFormParams}} + export type {{nickname}}BodyType = {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}ApiTypes.{{nickname}}BodyType + {{/hasFormParams}} + export type {{nickname}}QueryParameters = {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}ApiTypes.{{nickname}}QueryParameters + export type {{nickname}}PathParameters = {{#lambda.titlecase}}{{#lambda.camelcase}}{{appName}}{{/lambda.camelcase}}{{/lambda.titlecase}}ApiTypes.{{nickname}}PathParameters + {{/vendorExtensions}} {{/operation}} {{/operations}} {{/apis}}