Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type system for get and head methods #87

Merged
merged 7 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 13 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

type JSONObject = {[key: string]: JSONValue};
interface JSONArray extends Array<JSONValue> {}
export type JSONValue = string | number | boolean | null | JSONObject | JSONArray;
Expand Down Expand Up @@ -76,6 +78,14 @@ export interface Options extends RequestInit {
throwHttpErrors?: boolean;
}

interface OptionsWithoutBody extends Omit<Options, 'body'> {
method?: 'get' | 'head'
}

interface OptionsWithBody extends Options {
method?: 'post' | 'put' | 'delete'
}

/**
* Returns a `Response` object with `Body` methods added for convenience.
*/
Expand Down Expand Up @@ -106,15 +116,15 @@ export interface Ky {
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
(input: Request | URL | string, options?: Options): ResponsePromise;
(input: Request | URL | string, options?: OptionsWithoutBody | OptionsWithBody): ResponsePromise;

/**
* Fetches the `input` URL with the option `{method: 'get'}`.
*
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
get(input: Request | URL | string, options?: Options): ResponsePromise;
get(input: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise;

/**
* Fetches the `input` URL with the option `{method: 'post'}`.
Expand Down Expand Up @@ -146,7 +156,7 @@ export interface Ky {
* @param input - `Request` object, `URL` object, or URL string.
* @returns Promise with `Body` method added.
*/
head(input: Request | URL | string, options?: Options): ResponsePromise;
head(input: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise;

/**
* Fetches the `input` URL with the option `{method: 'delete'}`.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ class Ky {
const headers = new Headers(this._options.headers || {});

if (json) {
if (this._options.body) {
throw new TypeError('The `json` option cannot be used with the `body` option');
doniyor2109 marked this conversation as resolved.
Show resolved Hide resolved
}

headers.set('content-type', 'application/json');
this._options.body = JSON.stringify(json);
}
Expand Down
4 changes: 4 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ test('POST JSON', async t => {
await server.close();
});

test('cannot use `json` option along with the `body` option', t => {
t.throws(() => ky('https://example.com', {json: {foo: 'bar'}, body: 'foobar'}), 'The `json` option cannot be used with the `body` option');
});

test('custom headers', async t => {
const server = await createTestServer();
server.get('/', (request, response) => {
Expand Down