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

introducing OmitReadonly type to resolve required + readonly/writeonly properties #1145

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/templates/core/utils/OmitReadonly.hbs
@@ -0,0 +1,47 @@
/**
* The contents of this file are inspired from https://github.com/ts-essentials/ts-essentials#ReadonlyKeys
*
* The MIT License
*
* Copyright (c) 2018-2019 Chris Kaczor (github.com/krzkaczor)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* */

type Writable<T> = { -readonly [P in keyof T]: T[P] };

type IsEqualConsideringWritability<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
? true
: false;

type IsFullyWritable<T extends object> = IsEqualConsideringWritability<
{ [Q in keyof T]: T[Q] },
Writable<{ [Q in keyof T]: T[Q] }>
>;

/** Gets keys of an object which are readonly */
type ReadonlyKeys<T extends object> = {
[P in keyof T]-?: IsFullyWritable<Pick<T, P>> extends true ? never : P;
}[keyof T];

/**
* Exclude keys of an object which are readonly
* In case of union types containing types not extending object, type is inferred as-is
* */
export declare type OmitReadonly<T> = T extends object ? Omit<T, ReadonlyKeys<T>> : T;
1 change: 1 addition & 0 deletions src/templates/exportService.hbs
Expand Up @@ -30,6 +30,7 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest';
import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
{{/if}}
import type { OmitReadonly } from '../core/utils/OmitReadonly';

{{#equals @root.httpClient 'angular'}}
@Injectable()
Expand Down
4 changes: 2 additions & 2 deletions src/templates/partials/parameters.hbs
Expand Up @@ -16,13 +16,13 @@
{{/if}}
*/
{{/ifdef}}
{{{name}}}{{>isRequired}}: {{>type}},
{{{name}}}{{>isRequired}}: {{>typeWithOmitReadOnly httpMethod=../method}},
{{/each}}
}
{{~else}}

{{#each parameters}}
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
{{{name}}}{{>isRequired}}: {{>typeWithOmitReadOnly httpMethod=../method}}{{#if default}} = {{{default}}}{{/if}},
{{/each}}
{{/if}}
{{/if}}
13 changes: 13 additions & 0 deletions src/templates/partials/typeWithOmitReadOnly.hbs
@@ -0,0 +1,13 @@
{{#equals export "reference"}}
{{#equals httpMethod "POST"~}}
OmitReadonly<{{>type}}>
{{~else equals httpMethod "PUT"~}}
OmitReadonly<{{>type}}>
{{~else equals httpMethod "PATCH"~}}
OmitReadonly<{{>type}}>
{{else}}
{{~>type}}
{{~/equals}}
{{~else}}
{{~>type}}
{{~/equals}}
9 changes: 9 additions & 0 deletions src/utils/registerHandlebarTemplates.ts
Expand Up @@ -46,6 +46,7 @@ import nodeRequest from '../templates/core/node/request.hbs';
import nodeSendRequest from '../templates/core/node/sendRequest.hbs';
import templateCoreSettings from '../templates/core/OpenAPI.hbs';
import templateCoreRequest from '../templates/core/request.hbs';
import omitReadonly from '../templates/core/utils/OmitReadonly.hbs';
import xhrGetHeaders from '../templates/core/xhr/getHeaders.hbs';
import xhrGetRequestBody from '../templates/core/xhr/getRequestBody.hbs';
import xhrGetResponseBody from '../templates/core/xhr/getResponseBody.hbs';
Expand Down Expand Up @@ -83,6 +84,7 @@ import partialTypeInterface from '../templates/partials/typeInterface.hbs';
import partialTypeIntersection from '../templates/partials/typeIntersection.hbs';
import partialTypeReference from '../templates/partials/typeReference.hbs';
import partialTypeUnion from '../templates/partials/typeUnion.hbs';
import typeWithOmitReadOnly from '../templates/partials/typeWithOmitReadOnly.hbs';
import { registerHandlebarHelpers } from './registerHandlebarHelpers';

export interface Templates {
Expand All @@ -102,6 +104,9 @@ export interface Templates {
request: Handlebars.TemplateDelegate;
baseHttpRequest: Handlebars.TemplateDelegate;
httpRequest: Handlebars.TemplateDelegate;
utils: {
omitReadonly: Handlebars.TemplateDelegate;
};
};
}

Expand Down Expand Up @@ -134,6 +139,9 @@ export const registerHandlebarTemplates = (root: {
request: Handlebars.template(templateCoreRequest),
baseHttpRequest: Handlebars.template(templateCoreBaseHttpRequest),
httpRequest: Handlebars.template(templateCoreHttpRequest),
utils: {
omitReadonly: Handlebars.template(omitReadonly),
},
},
};

Expand Down Expand Up @@ -163,6 +171,7 @@ export const registerHandlebarTemplates = (root: {
Handlebars.registerPartial('typeInterface', Handlebars.template(partialTypeInterface));
Handlebars.registerPartial('typeReference', Handlebars.template(partialTypeReference));
Handlebars.registerPartial('typeUnion', Handlebars.template(partialTypeUnion));
Handlebars.registerPartial('typeWithOmitReadOnly', Handlebars.template(typeWithOmitReadOnly));
Handlebars.registerPartial('typeIntersection', Handlebars.template(partialTypeIntersection));
Handlebars.registerPartial('base', Handlebars.template(partialBase));

Expand Down
3 changes: 3 additions & 0 deletions src/utils/writeClient.spec.ts
Expand Up @@ -33,6 +33,9 @@ describe('writeClient', () => {
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
httpRequest: () => 'httpRequest',
utils: {
omitReadonly: () => 'omitReadonly',
},
},
};

Expand Down
14 changes: 12 additions & 2 deletions src/utils/writeClient.ts
Expand Up @@ -50,6 +50,7 @@ export const writeClient = async (
): Promise<void> => {
const outputPath = resolve(process.cwd(), output);
const outputPathCore = resolve(outputPath, 'core');
const outputPathCoreUtils = resolve(outputPath, 'core', 'utils');
const outputPathModels = resolve(outputPath, 'models');
const outputPathSchemas = resolve(outputPath, 'schemas');
const outputPathServices = resolve(outputPath, 'services');
Expand All @@ -60,8 +61,17 @@ export const writeClient = async (

if (exportCore) {
await rmdir(outputPathCore);
await mkdir(outputPathCore);
await writeClientCore(client, templates, outputPathCore, httpClient, indent, clientName, request);
await mkdir(outputPathCoreUtils);
await writeClientCore(
client,
templates,
outputPathCore,
outputPathCoreUtils,
httpClient,
indent,
clientName,
request
);
}

if (exportServices) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/writeClientClass.spec.ts
Expand Up @@ -33,6 +33,9 @@ describe('writeClientClass', () => {
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
httpRequest: () => 'httpRequest',
utils: {
omitReadonly: () => 'omitReadonly',
},
},
};

Expand Down
6 changes: 5 additions & 1 deletion src/utils/writeClientCore.spec.ts
Expand Up @@ -35,16 +35,20 @@ describe('writeClientCore', () => {
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
httpRequest: () => 'httpRequest',
utils: {
omitReadonly: () => 'omitReadonly',
},
},
};

await writeClientCore(client, templates, '/', HttpClient.FETCH, Indent.SPACE_4);
await writeClientCore(client, templates, '/', '/utils', HttpClient.FETCH, Indent.SPACE_4);

expect(writeFile).toBeCalledWith('/OpenAPI.ts', `settings${EOL}`);
expect(writeFile).toBeCalledWith('/ApiError.ts', `apiError${EOL}`);
expect(writeFile).toBeCalledWith('/ApiRequestOptions.ts', `apiRequestOptions${EOL}`);
expect(writeFile).toBeCalledWith('/ApiResult.ts', `apiResult${EOL}`);
expect(writeFile).toBeCalledWith('/CancelablePromise.ts', `cancelablePromise${EOL}`);
expect(writeFile).toBeCalledWith('/request.ts', `request${EOL}`);
expect(writeFile).toBeCalledWith('/utils/OmitReadonly.ts', `omitReadonly${EOL}`);
});
});
3 changes: 3 additions & 0 deletions src/utils/writeClientCore.ts
Expand Up @@ -14,6 +14,7 @@ import type { Templates } from './registerHandlebarTemplates';
* @param client Client object, containing, models, schemas and services
* @param templates The loaded handlebar templates
* @param outputPath Directory to write the generated files to
* @param outputUtilsPath Directory to write the generated util files to
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
* @param indent Indentation options (4, 2 or tab)
* @param clientName Custom client class name
Expand All @@ -23,6 +24,7 @@ export const writeClientCore = async (
client: Client,
templates: Templates,
outputPath: string,
outputUtilsPath: string,
httpClient: HttpClient,
indent: Indent,
clientName?: string,
Expand All @@ -43,6 +45,7 @@ export const writeClientCore = async (
await writeFile(resolve(outputPath, 'ApiResult.ts'), i(templates.core.apiResult(context), indent));
await writeFile(resolve(outputPath, 'CancelablePromise.ts'), i(templates.core.cancelablePromise(context), indent));
await writeFile(resolve(outputPath, 'request.ts'), i(templates.core.request(context), indent));
await writeFile(resolve(outputUtilsPath, 'OmitReadonly.ts'), i(templates.core.utils.omitReadonly(context), indent));

if (isDefined(clientName)) {
await writeFile(resolve(outputPath, 'BaseHttpRequest.ts'), i(templates.core.baseHttpRequest(context), indent));
Expand Down
3 changes: 3 additions & 0 deletions src/utils/writeClientIndex.spec.ts
Expand Up @@ -31,6 +31,9 @@ describe('writeClientIndex', () => {
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
httpRequest: () => 'httpRequest',
utils: {
omitReadonly: () => 'omitReadonly',
},
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/utils/writeClientModels.spec.ts
Expand Up @@ -48,6 +48,9 @@ describe('writeClientModels', () => {
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
httpRequest: () => 'httpRequest',
utils: {
omitReadonly: () => 'omitReadonly',
},
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/utils/writeClientSchemas.spec.ts
Expand Up @@ -48,6 +48,9 @@ describe('writeClientSchemas', () => {
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
httpRequest: () => 'httpRequest',
utils: {
omitReadonly: () => 'omitReadonly',
},
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/utils/writeClientServices.spec.ts
Expand Up @@ -36,6 +36,9 @@ describe('writeClientServices', () => {
request: () => 'request',
baseHttpRequest: () => 'baseHttpRequest',
httpRequest: () => 'httpRequest',
utils: {
omitReadonly: () => 'omitReadonly',
},
},
};

Expand Down