Skip to content

Commit

Permalink
feat(api-client): support for head, options and patch http methods
Browse files Browse the repository at this point in the history
Signed-off-by: Vojtech Masek <vojtech.masek@flowup.cz>
  • Loading branch information
vmasek committed Mar 12, 2018
1 parent 37e358b commit bd240f5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/parser.ts
Expand Up @@ -31,12 +31,12 @@ export function createMustacheViewModel(swagger: Swagger): MustacheData {
}

function parseMethods({paths, security, parameters}: Swagger): Method[] {
const authorizedMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
const supportedMethods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'];

return [].concat.apply([], Object.entries(paths)
.map(([pathName, apiPath]) => Object.entries(apiPath)
.filter(([methodType,]) => // tslint:disable-line:whitespace
authorizedMethods.indexOf(methodType.toUpperCase()) !== -1 // skip unsupported methods
supportedMethods.indexOf(methodType.toUpperCase()) !== -1 // skip unsupported methods
)
.map(([methodType, operation]) => ({
path: pathName.replace(/({.*?})/g, '$$$1'), // turn path interpolation `{this}` into string template `${this}
Expand Down
14 changes: 10 additions & 4 deletions templates/ngx-service.mustache
Expand Up @@ -68,14 +68,20 @@ export class APIClient {
{{/methods}}
private sendRequest<T>(method: string, path: string, options: HttpOptions, body?: any): Observable<T> {
switch (method) {
case 'DELETE':
return this.http.delete<T>(`${this.domain}${path}`, options);
case 'GET':
return this.http.get<T>(`${this.domain}${path}`, options);
case 'PUT':
return this.http.put<T>(`${this.domain}${path}`, body, options);
case 'HEAD':
return this.http.head<T>(`${this.domain}${path}`, options);
case 'OPTIONS':
return this.http.options<T>(`${this.domain}${path}`, options);
case 'PATCH':
return this.http.patch<T>(`${this.domain}${path}`, body, options);
case 'POST':
return this.http.post<T>(`${this.domain}${path}`, body, options);
case 'DELETE':
return this.http.delete<T>(`${this.domain}${path}`, options);
case 'PUT':
return this.http.put<T>(`${this.domain}${path}`, body, options);
default:
console.error(`Unsupported request: ${method}`);
return Observable.throw(`Unsupported request: ${method}`);
Expand Down
14 changes: 10 additions & 4 deletions tests/esquare/api/api-client.service.ts
Expand Up @@ -551,14 +551,20 @@ export class APIClient {

private sendRequest<T>(method: string, path: string, options: HttpOptions, body?: any): Observable<T> {
switch (method) {
case 'DELETE':
return this.http.delete<T>(`${this.domain}${path}`, options);
case 'GET':
return this.http.get<T>(`${this.domain}${path}`, options);
case 'PUT':
return this.http.put<T>(`${this.domain}${path}`, body, options);
case 'HEAD':
return this.http.head<T>(`${this.domain}${path}`, options);
case 'OPTIONS':
return this.http.options<T>(`${this.domain}${path}`, options);
case 'PATCH':
return this.http.patch<T>(`${this.domain}${path}`, body, options);
case 'POST':
return this.http.post<T>(`${this.domain}${path}`, body, options);
case 'DELETE':
return this.http.delete<T>(`${this.domain}${path}`, options);
case 'PUT':
return this.http.put<T>(`${this.domain}${path}`, body, options);
default:
console.error(`Unsupported request: ${method}`);
return Observable.throw(`Unsupported request: ${method}`);
Expand Down
14 changes: 10 additions & 4 deletions tests/gcloud-firestore/api/api-client.service.ts
Expand Up @@ -203,14 +203,20 @@ export class APIClient {

private sendRequest<T>(method: string, path: string, options: HttpOptions, body?: any): Observable<T> {
switch (method) {
case 'DELETE':
return this.http.delete<T>(`${this.domain}${path}`, options);
case 'GET':
return this.http.get<T>(`${this.domain}${path}`, options);
case 'PUT':
return this.http.put<T>(`${this.domain}${path}`, body, options);
case 'HEAD':
return this.http.head<T>(`${this.domain}${path}`, options);
case 'OPTIONS':
return this.http.options<T>(`${this.domain}${path}`, options);
case 'PATCH':
return this.http.patch<T>(`${this.domain}${path}`, body, options);
case 'POST':
return this.http.post<T>(`${this.domain}${path}`, body, options);
case 'DELETE':
return this.http.delete<T>(`${this.domain}${path}`, options);
case 'PUT':
return this.http.put<T>(`${this.domain}${path}`, body, options);
default:
console.error(`Unsupported request: ${method}`);
return Observable.throw(`Unsupported request: ${method}`);
Expand Down

0 comments on commit bd240f5

Please sign in to comment.