Skip to content

Commit

Permalink
fix: handle async response transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Jul 1, 2024
1 parent 0a31408 commit 8410046
Show file tree
Hide file tree
Showing 52 changed files with 109 additions and 92 deletions.
6 changes: 6 additions & 0 deletions .changeset/two-cooks-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hey-api/client-fetch': patch
'@hey-api/openapi-ts': patch
---

fix: handle async response transformers
2 changes: 1 addition & 1 deletion packages/client-axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export const request = <T>(

let transformedBody = responseBody;
if (options.responseTransformer && isSuccess(response.status)) {
transformedBody = options.responseTransformer(responseBody);
transformedBody = await options.responseTransformer(responseBody);
}

const result: ApiResult = {
Expand Down
2 changes: 1 addition & 1 deletion packages/client-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type ApiRequestOptions<T = unknown> = {
readonly path?: Record<string, unknown>;
readonly query?: Record<string, unknown>;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly url: string;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/client-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const createClient = (config: Config): Client => {

let data = await response[parseAs]();
if (parseAs === 'json' && options.responseTransformer) {
data = options.responseTransformer(data);
data = await options.responseTransformer(data);
}

return {
Expand Down
8 changes: 6 additions & 2 deletions packages/client-fetch/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ export interface Config
* {@link https://swagger.io/docs/specification/serialization/#query View examples}
*/
querySerializer?: QuerySerializer | QuerySerializerOptions;

responseTransformer?: (data: unknown) => unknown;
/**
* A function for transforming response data before it's returned to the
* caller function. This is an ideal place to post-process server data,
* e.g. convert date ISO strings into native Date objects.
*/
responseTransformer?: (data: unknown) => Promise<unknown>;
}

interface RequestOptionsBase extends Omit<Config, 'global'> {
Expand Down
4 changes: 3 additions & 1 deletion packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,22 @@ export const toParameterDeclarations = (parameters: FunctionParameter[]) =>
* Create Function type expression.
*/
export const createFunction = ({
async,

Check warning on line 111 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L111

Added line #L111 was not covered by tests
comment,
multiLine,
parameters = [],
returnType,
statements = [],
}: {
async?: boolean;

Check warning on line 118 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L118

Added line #L118 was not covered by tests
comment?: Comments;
multiLine?: boolean;
parameters?: FunctionParameter[];
returnType?: string | ts.TypeNode;
statements?: ts.Statement[];
}) => {
const expression = ts.factory.createArrowFunction(
undefined,
async ? [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)] : undefined,

Check warning on line 126 in packages/openapi-ts/src/compiler/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/compiler/types.ts#L126

Added line #L126 was not covered by tests
undefined,
toParameterDeclarations(parameters),
returnType ? createTypeNode(returnType) : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/templates/core/angular/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: Api

let transformedBody = responseBody;
if (options.responseTransformer && response.ok) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/templates/core/axios/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>,

let transformedBody = responseBody;
if (options.responseTransformer && isSuccess(response.status)) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}

const result: ApiResult = {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/templates/core/fetch/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>)

let transformedBody = responseBody;
if (options.responseTransformer && response.ok) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}

const result: ApiResult = {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/templates/core/xhr/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>)

let transformedBody = responseBody;
if (options.responseTransformer && isSuccess(response.status)) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}

const result: ApiResult = {
Expand Down
7 changes: 6 additions & 1 deletion packages/openapi-ts/src/utils/write/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const ensureModelResponseTransformerExists = (
});
generateResponseTransformer({
...props,
async: false,

Check warning on line 69 in packages/openapi-ts/src/utils/write/transformers.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/transformers.ts#L69

Added line #L69 was not covered by tests
name,
statements,
});
Expand Down Expand Up @@ -179,12 +180,14 @@ const processModel = (props: ModelProps): ts.Statement[] => {
};

const generateResponseTransformer = ({
async,

Check warning on line 183 in packages/openapi-ts/src/utils/write/transformers.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/transformers.ts#L183

Added line #L183 was not covered by tests
client,
name,
onNode,
onRemoveNode,
statements,
}: Pick<TypesProps, 'client' | 'onNode' | 'onRemoveNode'> & {
async: boolean;

Check warning on line 190 in packages/openapi-ts/src/utils/write/transformers.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/transformers.ts#L190

Added line #L190 was not covered by tests
name: string;
statements: Array<ts.Statement>;
}) => {
Expand All @@ -205,6 +208,7 @@ const generateResponseTransformer = ({
}

const expression = compiler.types.function({
async,

Check warning on line 211 in packages/openapi-ts/src/utils/write/transformers.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/transformers.ts#L211

Added line #L211 was not covered by tests
multiLine: true,
parameters: [
{
Expand Down Expand Up @@ -313,6 +317,7 @@ export const processResponseTransformers = async ({
path: [dataVariableName],
});
generateResponseTransformer({
async: true,

Check warning on line 320 in packages/openapi-ts/src/utils/write/transformers.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/transformers.ts#L320

Added line #L320 was not covered by tests
client,
name: nameCreated,
onNode,
Expand All @@ -321,7 +326,7 @@ export const processResponseTransformers = async ({
});
},
onNode,
type: `(${dataVariableName}: any) => ${name}`,
type: `(${dataVariableName}: any) => Promise<${name}>`,

Check warning on line 329 in packages/openapi-ts/src/utils/write/transformers.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/utils/write/transformers.ts#L329

Added line #L329 was not covered by tests
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>)
let transformedBody = responseBody;
if (options.responseTransformer && response.ok) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}
const result: ApiResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>)
let transformedBody = responseBody;
if (options.responseTransformer && response.ok) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}
const result: ApiResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: Api

let transformedBody = responseBody;
if (options.responseTransformer && response.ok) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: Api

let transformedBody = responseBody;
if (options.responseTransformer && response.ok) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export type $OpenApiTs = {
};
};

export type ParentModelWithDatesResponseTransformer = (data: any) => ParentModelWithDatesResponse;
export type ParentModelWithDatesResponseTransformer = (data: any) => Promise<ParentModelWithDatesResponse>;

export type ParentModelWithDatesModelResponseTransformer = (data: any) => ParentModelWithDates;

Expand Down Expand Up @@ -147,23 +147,23 @@ export const ParentModelWithDatesModelResponseTransformer: ParentModelWithDatesM
return data;
};

export const ParentModelWithDatesResponseTransformer: ParentModelWithDatesResponseTransformer = data => {
export const ParentModelWithDatesResponseTransformer: ParentModelWithDatesResponseTransformer = async (data) => {
if (data) {
ParentModelWithDatesModelResponseTransformer(data);
}
return data;
};

export type ModelWithDatesResponseTransformer = (data: any) => ModelWithDatesResponse;
export type ModelWithDatesResponseTransformer = (data: any) => Promise<ModelWithDatesResponse>;

export const ModelWithDatesResponseTransformer: ModelWithDatesResponseTransformer = data => {
export const ModelWithDatesResponseTransformer: ModelWithDatesResponseTransformer = async (data) => {
ModelWithDatesModelResponseTransformer(data);
return data;
};

export type ModelWithDatesArrayResponseTransformer = (data: any) => ModelWithDatesArrayResponse;
export type ModelWithDatesArrayResponseTransformer = (data: any) => Promise<ModelWithDatesArrayResponse>;

export const ModelWithDatesArrayResponseTransformer: ModelWithDatesArrayResponseTransformer = data => {
export const ModelWithDatesArrayResponseTransformer: ModelWithDatesArrayResponseTransformer = async (data) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDatesModelResponseTransformer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>,
let transformedBody = responseBody;
if (options.responseTransformer && isSuccess(response.status)) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}
const result: ApiResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>,
let transformedBody = responseBody;
if (options.responseTransformer && isSuccess(response.status)) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}
const result: ApiResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export type $OpenApiTs = {
};
};

export type ParentModelWithDatesResponseTransformer = (data: any) => ParentModelWithDatesResponse;
export type ParentModelWithDatesResponseTransformer = (data: any) => Promise<ParentModelWithDatesResponse>;

export type ParentModelWithDatesModelResponseTransformer = (data: any) => ParentModelWithDates;

Expand Down Expand Up @@ -147,23 +147,23 @@ export const ParentModelWithDatesModelResponseTransformer: ParentModelWithDatesM
return data;
};

export const ParentModelWithDatesResponseTransformer: ParentModelWithDatesResponseTransformer = data => {
export const ParentModelWithDatesResponseTransformer: ParentModelWithDatesResponseTransformer = async (data) => {
if (data) {
ParentModelWithDatesModelResponseTransformer(data);
}
return data;
};

export type ModelWithDatesResponseTransformer = (data: any) => ModelWithDatesResponse;
export type ModelWithDatesResponseTransformer = (data: any) => Promise<ModelWithDatesResponse>;

export const ModelWithDatesResponseTransformer: ModelWithDatesResponseTransformer = data => {
export const ModelWithDatesResponseTransformer: ModelWithDatesResponseTransformer = async (data) => {
ModelWithDatesModelResponseTransformer(data);
return data;
};

export type ModelWithDatesArrayResponseTransformer = (data: any) => ModelWithDatesArrayResponse;
export type ModelWithDatesArrayResponseTransformer = (data: any) => Promise<ModelWithDatesArrayResponse>;

export const ModelWithDatesArrayResponseTransformer: ModelWithDatesArrayResponseTransformer = data => {
export const ModelWithDatesArrayResponseTransformer: ModelWithDatesArrayResponseTransformer = async (data) => {
if (Array.isArray(data)) {
data.forEach(ModelWithDatesModelResponseTransformer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>)
let transformedBody = responseBody;
if (options.responseTransformer && response.ok) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}
const result: ApiResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type ApiRequestOptions<T = unknown> = {
readonly body?: any;
readonly mediaType?: string;
readonly responseHeader?: string;
readonly responseTransformer?: (data: unknown) => T;
readonly responseTransformer?: (data: unknown) => Promise<T>;
readonly errors?: Record<number | string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions<T>)
let transformedBody = responseBody;
if (options.responseTransformer && response.ok) {
transformedBody = options.responseTransformer(responseBody)
transformedBody = await options.responseTransformer(responseBody)
}
const result: ApiResult = {
Expand Down
Loading

0 comments on commit 8410046

Please sign in to comment.