Skip to content

Commit

Permalink
Implement CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bergundy committed Feb 20, 2019
1 parent 36fac1e commit 2c7a49e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/test/test_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ main().catch((err) => {
});

try {
// Hacky and ugly way to get the listen ports.
// It's done this way to avoid a possible race when passing in a pre allocated port.
let webpackPort: number | undefined;

const serverUrl = await new Promise((resolve, reject) => {
Expand Down
3 changes: 2 additions & 1 deletion src/test/test_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@ export default class Handler {
import { TestClient } from './client';
export default async function test(client: TestClient) {
await expect(client.bar('yay', { timeoutMs: 100 })).to.eventually.be.rejectedWith(Error, 'The user aborted a request.');
await expect(client.bar('yay', { timeoutMs: 100 })).to.eventually.be.rejectedWith(
Error, 'Request aborted due to timeout on method "bar"');
}
`;
await new TestCase(dummySchema, handler, tester).run();
Expand Down
71 changes: 41 additions & 30 deletions templates/ts/client-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import {
{{/bypassTypes}}
} from './interfaces';

export class TimeoutError extends Error {
}

export {
ValidationError,
};

export interface Options extends Pick<RequestInit, 'agent' | 'redirect' | 'follow' | 'compress'> {
fetchImplementation?: typeof fetch;
timeoutMs?: number;
headers?: Record<string, string>;
}
Expand Down Expand Up @@ -82,40 +86,47 @@ export class {{name}}Client {
delete mergedOptions.timeoutMs;
}

const response = await fetch(`${this.serverUrl}/{{name}}`, {
...mergedOptions,
headers: {
...mergedOptions.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
method: 'POST',
});
const isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
if (response.status >= 200 && response.status < 300) {
const validator = this.validators.{{{name}}};
const wrapped = { returns: isJSON ? await response.json() : undefined }; // wrapped for coersion
if (!validator(wrapped)) {
throw new ValidationError('Failed to validate response', validator.errors);
try {
const response = await (mergedOptions.fetchImplementation || fetch)(`${this.serverUrl}/{{name}}`, {
...mergedOptions,
headers: {
...mergedOptions.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
method: 'POST',
});
const isJSON = (response.headers.get('content-type') || '').startsWith('application/json');
if (response.status >= 200 && response.status < 300) {
const validator = this.validators.{{{name}}};
const wrapped = { returns: isJSON ? await response.json() : undefined }; // wrapped for coersion
if (!validator(wrapped)) {
throw new ValidationError('Failed to validate response', validator.errors);
}
return wrapped.returns as {{{returnType}}};
} else if (!isJSON) {
throw new Error(`${response.status} - ${response.statusText}`);
} else if (response.status === 400) {
const body = await response.json();
if (body.name === 'ValidationError') {
throw new ValidationError(body.message, body.errors);
}
} else if (response.status === 500) {
const body = await response.json();
{{#throws}}
if (body.name === '{{.}}') {
throw new {{.}}(body.message);
}
{{/throws}}
throw new InternalServerError(body.message);
}
return wrapped.returns as {{{returnType}}};
} else if (!isJSON) {
throw new Error(`${response.status} - ${response.statusText}`);
} else if (response.status === 400) {
const body = await response.json();
if (body.name === 'ValidationError') {
throw new ValidationError(body.message, body.errors);
}
} else if (response.status === 500) {
const body = await response.json();
{{#throws}}
if (body.name === '{{.}}') {
throw new {{.}}(body.message);
} catch (err) {
if (err.message === 'The user aborted a request.') {
throw new TimeoutError('Request aborted due to timeout on method "{{{name}}}"');
}
{{/throws}}
throw new InternalServerError(body.message);
throw err;
}
throw new Error(`${response.status} - ${response.statusText}`);
}
{{/methods}}
}
Expand Down

0 comments on commit 2c7a49e

Please sign in to comment.