Skip to content

Commit

Permalink
handleArrays option added.
Browse files Browse the repository at this point in the history
  • Loading branch information
houd1ni committed Jul 25, 2019
1 parent 5ddc532 commit 0809ecb
Show file tree
Hide file tree
Showing 11 changed files with 1,602 additions and 102 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@ const api = new Fetch({
// Automatically turns response into an object.
// Otherwise, returns raw response from fetch API:
// do `await response.json() to get it.
json: true,
json?: true,
// Common base url. E.g. your api domain.
base: '/',
base?: '/',
// Object with headers.
headers: {},
headers?: {},
// List of async middlewares executed from last to first
// procesing queries.
middleware: {
in: [], // ({query, response}) => Promise<{query, response}>
out: [] // (query: Query) => Promise<Query>
middleware?: {
in?: [], // ({query, response}) => Promise<{query, response}>
out?: [] // (query: Query) => Promise<Query>
},
credentials: 'omit' | 'same-origin' | 'include', // fetch API credentials field.
throwCodes: /5../ // HTTP status codes to throw. Defaults to /\w/ (no throws at all).
credentials?: 'omit' | 'same-origin' | 'include', // fetch API credentials field.
throwCodes?: /5../, // HTTP status codes to throw. Defaults to /\w/ (no throws at all).
handleArrays?: '[]' // querystring: arr[]=1&arr[]=2&... vs arr=1,2,...
})
```
*Query is of type*
```typescript
interface Query {
url: string
method: RESTMethods // e.g. 'get' or 'post'
method?: RESTMethods // e.g. 'get' or 'post'
// Prevents query from going to server and returns this.
// Useful for mocks.
result?: any
Expand All @@ -47,7 +48,8 @@ interface Query {
headers?: Headers
json?: boolean
credentials?: 'omit', // fetch API credentials field. Same as in the config.
throwCodes: /5../ // Status codes to throw. Same as in the config.
throwCodes?: /5../, // Status codes to throw. Same as in the config.
handleArrays?: '[]' // querystring: arr[]=1&arr[]=2&... vs arr=1,2,...
}
```

Expand Down
4 changes: 4 additions & 0 deletions dist/bundle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Headers {
[name: string]: string | null;
}
export declare type Method = 'get' | 'post' | 'put' | 'head' | 'delete' | 'options' | 'trace' | 'connect';
export declare type HandleArrays = '[]' | ',';
export declare type OutMiddleware = (query: Query) => Promise<Query>;
export declare type InMiddleware = ({ query: Query, response: any }: {
query: any;
Expand All @@ -24,6 +25,7 @@ export interface Config {
timeout: number;
credentials: Credentials;
throwCodes: RegExp;
handleArrays: HandleArrays;
middleware: {
in?: InMiddleware[];
out?: OutMiddleware[];
Expand All @@ -46,6 +48,7 @@ export interface Query {
json: boolean;
timeout: number;
misc: AnyObject;
handleArrays: HandleArrays;
}
export interface FetchData {
method: Method;
Expand Down Expand Up @@ -104,6 +107,7 @@ export declare class ServerError extends FetchError {
name: string;
constructor(response: Response);
}
/** Turns query params into query string. */
export declare const formURI: (query: Partial<Query>) => string;

export {};
2 changes: 1 addition & 1 deletion dist/bundle.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

Loading

0 comments on commit 0809ecb

Please sign in to comment.