Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-poets-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': minor
---

feat: add namespace supporting for enums
1 change: 1 addition & 0 deletions packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export const compiler = {
array: types.createArrayType,
enum: types.createEnumDeclaration,
function: types.createFunction,
namespace: types.createNamespaceDeclaration,
object: types.createObjectType,
},
utils: {
Expand Down
22 changes: 22 additions & 0 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,25 @@ export const createEnumDeclaration = <T extends object>({
}
return declaration;
};

/**
* Create namespace declaration. Example `export namespace MyNamespace { ... }`
* @param name - the name of the namespace.
* @param nodes - the nodes in the namespace.
* @returns
*/
export const createNamespaceDeclaration = <
T extends Array<ts.EnumDeclaration>,
>({
name,
statements,
}: {
name: string;
statements: T;
}) =>
ts.factory.createModuleDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createIdentifier(name),
ts.factory.createModuleBlock(statements),
ts.NodeFlags.Namespace,
);
63 changes: 58 additions & 5 deletions packages/openapi-ts/src/generate/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EnumDeclaration } from 'typescript';

import {
type Comments,
compiler,
Expand Down Expand Up @@ -121,10 +123,34 @@ export const generateType = ({
};

const processComposition = (props: TypesProps) => {
processType(props);
props.model.enums.forEach((enumerator) =>
processEnum({ ...props, model: enumerator }),
);
const config = getConfig();

if (config.types.enums !== 'typescript+namespace') {
processType(props);
props.model.enums.forEach((enumerator) =>
processEnum({ ...props, model: enumerator }),
);
} else {
const enumDeclarations = [] as EnumDeclaration[];
props.model.enums.forEach((enumerator) =>
processScopedEnum({
...props,
model: enumerator,
onNode: (node) => {
enumDeclarations.push(node as EnumDeclaration);
},
}),
);
processType(props);
if (enumDeclarations.length > 0) {
props.onNode(
compiler.types.namespace({
name: props.model.name,
statements: enumDeclarations,
}),
);
}
}
};

const processEnum = ({ client, model, onNode }: TypesProps) => {
Expand All @@ -146,7 +172,10 @@ const processEnum = ({ client, model, onNode }: TypesProps) => {
model.deprecated && '@deprecated',
];

if (config.types.enums === 'typescript') {
if (
config.types.enums === 'typescript' ||
config.types.enums === 'typescript+namespace'
) {
generateEnum({
client,
comments,
Expand Down Expand Up @@ -185,6 +214,30 @@ const processEnum = ({ client, model, onNode }: TypesProps) => {
});
};

const processScopedEnum = ({ model, onNode }: TypesProps) => {
const properties: Record<string | number, unknown> = {};
const comments: Record<string | number, Comments> = {};
model.enum.forEach((enumerator) => {
const { key, value } = enumEntry(enumerator);
properties[key] = value;
const comment = enumerator.customDescription || enumerator.description;
if (comment) {
comments[key] = [escapeComment(comment)];
}
});
onNode(
compiler.types.enum({
comments,
leadingComment: [
model.description && escapeComment(model.description),
model.deprecated && '@deprecated',
],
name: model.meta?.name || model.name,
obj: properties,
}),
);
};

const processType = ({ client, model, onNode }: TypesProps) => {
generateType({
client,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export interface ClientConfig {
* Generate enum definitions?
* @default false
*/
enums?: 'javascript' | 'typescript' | false;
enums?: 'javascript' | 'typescript' | 'typescript+namespace' | false;
/**
* Generate types?
* @default true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { ApiRequestOptions } from './ApiRequestOptions';
import type { ApiResult } from './ApiResult';

export class ApiError extends Error {
public readonly url: string;
public readonly status: number;
public readonly statusText: string;
public readonly body: unknown;
public readonly request: ApiRequestOptions;

constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
super(message);

this.name = 'ApiError';
this.url = response.url;
this.status = response.status;
this.statusText = response.statusText;
this.body = response.body;
this.request = request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly cookies?: Record<string, unknown>;
readonly errors?: Record<number | string, string>;
readonly formData?: Record<string, unknown> | any[] | Blob | File;
readonly headers?: Record<string, unknown>;
readonly mediaType?: string;
readonly method:
| 'DELETE'
| 'GET'
| 'HEAD'
| 'OPTIONS'
| 'PATCH'
| 'POST'
| 'PUT';
readonly path?: Record<string, unknown>;
readonly query?: Record<string, unknown>;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly url: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type ApiResult<TData = any> = {
readonly body: TData;
readonly ok: boolean;
readonly status: number;
readonly statusText: string;
readonly url: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
export class CancelError extends Error {
constructor(message: string) {
super(message);
this.name = 'CancelError';
}

public get isCancelled(): boolean {
return true;
}
}

export interface OnCancel {
readonly isResolved: boolean;
readonly isRejected: boolean;
readonly isCancelled: boolean;

(cancelHandler: () => void): void;
}

export class CancelablePromise<T> implements Promise<T> {
private _isResolved: boolean;
private _isRejected: boolean;
private _isCancelled: boolean;
readonly cancelHandlers: (() => void)[];
readonly promise: Promise<T>;
private _resolve?: (value: T | PromiseLike<T>) => void;
private _reject?: (reason?: unknown) => void;

constructor(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: unknown) => void,
onCancel: OnCancel
) => void
) {
this._isResolved = false;
this._isRejected = false;
this._isCancelled = false;
this.cancelHandlers = [];
this.promise = new Promise<T>((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;

const onResolve = (value: T | PromiseLike<T>): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return;
}
this._isResolved = true;
if (this._resolve) this._resolve(value);
};

const onReject = (reason?: unknown): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return;
}
this._isRejected = true;
if (this._reject) this._reject(reason);
};

const onCancel = (cancelHandler: () => void): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
return;
}
this.cancelHandlers.push(cancelHandler);
};

Object.defineProperty(onCancel, 'isResolved', {
get: (): boolean => this._isResolved,
});

Object.defineProperty(onCancel, 'isRejected', {
get: (): boolean => this._isRejected,
});

Object.defineProperty(onCancel, 'isCancelled', {
get: (): boolean => this._isCancelled,
});

return executor(onResolve, onReject, onCancel as OnCancel);
});
}

get [Symbol.toStringTag]() {
return "Cancellable Promise";
}

public then<TResult1 = T, TResult2 = never>(
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this.promise.then(onFulfilled, onRejected);
}

public catch<TResult = never>(
onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null
): Promise<T | TResult> {
return this.promise.catch(onRejected);
}

public finally(onFinally?: (() => void) | null): Promise<T> {
return this.promise.finally(onFinally);
}

public cancel(): void {
if (this._isResolved || this._isRejected || this._isCancelled) {
return;
}
this._isCancelled = true;
if (this.cancelHandlers.length) {
try {
for (const cancelHandler of this.cancelHandlers) {
cancelHandler();
}
} catch (error) {
console.warn('Cancellation threw an error', error);
return;
}
}
this.cancelHandlers.length = 0;
if (this._reject) this._reject(new CancelError('Request aborted'));
}

public get isCancelled(): boolean {
return this._isCancelled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { ApiRequestOptions } from './ApiRequestOptions';

type Headers = Record<string, string>;
type Middleware<T> = (value: T) => T | Promise<T>;
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>;

export class Interceptors<T> {
_fns: Middleware<T>[];

constructor() {
this._fns = [];
}

eject(fn: Middleware<T>): void {
const index = this._fns.indexOf(fn);
if (index !== -1) {
this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)];
}
}

use(fn: Middleware<T>): void {
this._fns = [...this._fns, fn];
}
}

export type OpenAPIConfig = {
BASE: string;
CREDENTIALS: 'include' | 'omit' | 'same-origin';
ENCODE_PATH?: ((path: string) => string) | undefined;
HEADERS?: Headers | Resolver<Headers> | undefined;
PASSWORD?: string | Resolver<string> | undefined;
TOKEN?: string | Resolver<string> | undefined;
USERNAME?: string | Resolver<string> | undefined;
VERSION: string;
WITH_CREDENTIALS: boolean;
interceptors: {
request: Interceptors<RequestInit>;
response: Interceptors<Response>;
};
};

export const OpenAPI: OpenAPIConfig = {
BASE: 'http://localhost:3000/base',
CREDENTIALS: 'include',
ENCODE_PATH: undefined,
HEADERS: undefined,
PASSWORD: undefined,
TOKEN: undefined,
USERNAME: undefined,
VERSION: '1.0',
WITH_CREDENTIALS: false,
interceptors: {
request: new Interceptors(),
response: new Interceptors(),
},
};
Loading