Skip to content

Commit

Permalink
feat(api-client): body request based on the request type and the para…
Browse files Browse the repository at this point in the history
…meter

BREAKING CHANGE: generator now ignores bodies for requests that should not send any body, only PUT, PATCH and POST should contain body and it is handled as optional
  • Loading branch information
vmasek committed Dec 9, 2020
1 parent cdf2c6d commit d0309b0
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 32 deletions.
35 changes: 25 additions & 10 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ function parseMethods(
const op = <Operation>operation;
return (
supportedMethods.indexOf(methodType.toUpperCase()) !== -1 && // skip unsupported methods
(!swaggerTag || (op.tags && op.tags.includes(swaggerTag)))
); // if tag exists take only paths including this tag
(!swaggerTag || (op.tags && op.tags.includes(swaggerTag))) // if tag exists take only paths including this tag
);
})
.map(([methodType, operation]: [string, Operation]) => {
// select the lowest success (code 20x) response
Expand All @@ -143,23 +143,38 @@ function parseMethods(
parameters || {},
);

const methodTypeLowered = methodType.toLowerCase() as MethodType;
const formData = transformedParams
.filter(({ name, isFormParameter }) => name && isFormParameter)
.map(({ name, camelCaseName }) => ({
name,
camelCaseName: camelCaseName || name,
}));
const stringifyBody = (param?: Parameter) =>
param ? `JSON.stringify(args.${param.camelCaseName})` : 'null';
const body = /^(?:post|put|patch)\b/.test(methodTypeLowered)
? formData.length
? 'formData'
: stringifyBody(
transformedParams.find(
({ isBodyParameter }) => isBodyParameter,
),
)
: undefined;

return {
hasJsonResponse: true,
methodName: toCamelCase(
operation.operationId
? !swaggerTag
? operation.operationId
: operation.operationId.replace(`${swaggerTag}_`, '')
: `${methodType}_${pathName.replace(/[{}]/g, '')}`,
: `${methodTypeLowered}_${pathName.replace(/[{}]/g, '')}`,
),
methodType: methodType.toLowerCase() as MethodType,
methodType: methodTypeLowered,
body,
parameters: transformedParams,
formData: transformedParams
.filter(({ name, isFormParameter }) => name && isFormParameter)
.map(({ name, camelCaseName }) => ({
name,
camelCaseName: camelCaseName || name,
})),
formData,
// turn path interpolation `{this}` into string template `${args.this}
path: pathName.replace(
/{(.*?)}/g,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface Parameter extends Property {

export interface Method {
readonly path?: string; // path appended to base in method
readonly body?: string; // body that will be used for post, put and patch methods
readonly methodName?: string; // mane of the generated method
readonly methodType?: MethodType; // type of the http method
readonly description?: string;
Expand Down
2 changes: 1 addition & 1 deletion templates/ngx-service.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class {{#if (templateType 'guardedService')}}Guarded{{&serviceName}} exte
{{/formData}}

{{/if}}
return this.http.{{methodType}}<{{&responseTypeSchema.type}}>(`${this.domain}${path}`{{#parameters}}{{#isBodyParameter}}, JSON.stringify(args.{{&camelCaseName}}){{/isBodyParameter}}{{/parameters}}{{#if formData.length}}, formData{{/if}}, options);{{else}}
return this.http.{{methodType}}<{{&responseTypeSchema.type}}>(`${this.domain}${path}`{{#if body}}, {{body}}{{/if}}, options);{{else}}
return super.{{&methodName}}({{#if parameters.length}}args, {{/if}}requestHttpOptions, observe){{#responseGuard}}
.pipe(tap((res: any) => {{&../responseGuard}} || console.error(`TypeGuard for response '{{&../responseTypeSchema.type}}' caught inconsistency.`, res))){{/responseGuard}};{{/if}}
}{{else}};{{/if}}
Expand Down
1 change: 0 additions & 1 deletion tests/custom/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ paths:
get:
description: Get standard File
parameters:
- $ref: '#/parameters/modelParam'
- description: Name of repository owner.
in: path
name: owner
Expand Down
10 changes: 5 additions & 5 deletions tests/esquare/api/api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class APIClient implements APIClientInterface {
observe,
};

return this.http.post<object>(`${this.domain}${path}`, options);
return this.http.post<object>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -788,7 +788,7 @@ export class APIClient implements APIClientInterface {
observe,
};

return this.http.post<models.ImportResponse>(`${this.domain}${path}`, options);
return this.http.post<models.ImportResponse>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -895,7 +895,7 @@ export class APIClient implements APIClientInterface {
observe,
};

return this.http.post<void>(`${this.domain}${path}`, options);
return this.http.post<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -1748,7 +1748,7 @@ export class APIClient implements APIClientInterface {
observe,
};

return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -1781,7 +1781,7 @@ export class APIClient implements APIClientInterface {
observe,
};

return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/github/api/services/gists/gists-api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ export class GistsAPIClient implements GistsAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.post<void>(`${this.domain}${path}`, options);
return this.http.post<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -887,7 +887,7 @@ export class GistsAPIClient implements GistsAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class MarkdownAPIClient implements MarkdownAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.post<void>(`${this.domain}${path}`, options);
return this.http.post<void>(`${this.domain}${path}`, null, options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class NotificationsAPIClient implements NotificationsAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.patch<void>(`${this.domain}${path}`, options);
return this.http.patch<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/github/api/services/orgs/orgs-api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ export class OrgsAPIClient implements OrgsAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/github/api/services/repos/repos-api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ export class ReposAPIClient implements ReposAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -1224,7 +1224,7 @@ export class ReposAPIClient implements ReposAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.delete<models.DeleteFile>(`${this.domain}${path}`, JSON.stringify(args.body), options);
return this.http.delete<models.DeleteFile>(`${this.domain}${path}`, options);
}

/**
Expand Down Expand Up @@ -2939,7 +2939,7 @@ export class ReposAPIClient implements ReposAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.post<void>(`${this.domain}${path}`, options);
return this.http.post<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/github/api/services/teams/teams-api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export class TeamsAPIClient implements TeamsAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -606,7 +606,7 @@ export class TeamsAPIClient implements TeamsAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<models.TeamMembership>(`${this.domain}${path}`, options);
return this.http.put<models.TeamMembership>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -710,7 +710,7 @@ export class TeamsAPIClient implements TeamsAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/github/api/services/user/user-api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class UserAPIClient implements UserAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.delete<void>(`${this.domain}${path}`, JSON.stringify(args.body), options);
return this.http.delete<void>(`${this.domain}${path}`, options);
}

/**
Expand Down Expand Up @@ -580,7 +580,7 @@ export class UserAPIClient implements UserAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -1242,7 +1242,7 @@ export class UserAPIClient implements UserAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down Expand Up @@ -1450,7 +1450,7 @@ export class UserAPIClient implements UserAPIClientInterface {
if ('xGitHubRequestId' in args) {
options.headers = options.headers.set('X-GitHub-Request-Id', String(args.xGitHubRequestId));
}
return this.http.put<void>(`${this.domain}${path}`, options);
return this.http.put<void>(`${this.domain}${path}`, null, options);
}

/**
Expand Down

0 comments on commit d0309b0

Please sign in to comment.