Skip to content

Commit

Permalink
Merge pull request #49 from metsavaht/feature/add-abortcontroller-pol…
Browse files Browse the repository at this point in the history
…yfill

Add AbortController polyfill for @tg-resources/fetch-runtime
  • Loading branch information
jorgenader authored Feb 12, 2019
2 parents 206a6dd + d3e5d5a commit db2d053
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 36 deletions.
21 changes: 11 additions & 10 deletions packages/core/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AllowedPostMethods,
Attachments,
ConfigType,
Kwargs,
ObjectMap,
Query,
RequestConfig,
Expand Down Expand Up @@ -84,27 +85,27 @@ export abstract class Resource extends Route implements ResourceInterface {
}

public fetch = <
R = any, Params extends { [K in keyof Params]?: string } = {}
R = any, Params extends Kwargs<Params> = {}
>(kwargs?: Params | null, query?: Query | null, requestConfig?: RequestConfig | null): Promise<R> | any => {
return this._fetch<R, Params>(kwargs, query, requestConfig, 'get');
};

public head = <
R = any, Params extends { [K in keyof Params]?: string } = {}
R = any, Params extends Kwargs<Params> = {}
>(kwargs?: Params | null, query?: Query | null, requestConfig?: RequestConfig | null): Promise<R> | any => {
// istanbul ignore next: Tested in package that implement Resource
return this._fetch<R, Params>(kwargs, query, requestConfig, 'head');
};

public options = <
R = any, Params extends { [K in keyof Params]?: string } = {}
R = any, Params extends Kwargs<Params> = {}
>(kwargs?: Params | null, query?: Query | null, requestConfig?: RequestConfig | null): Promise<R> | any => {
// istanbul ignore next: Tested in package that implement Resource
return this._fetch<R, Params>(kwargs, query, requestConfig, 'options');
};

public post = <
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments | null, requestConfig?: RequestConfig | null
Expand All @@ -113,7 +114,7 @@ export abstract class Resource extends Route implements ResourceInterface {
};

public patch = <
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments | null, requestConfig?: RequestConfig | null
Expand All @@ -122,7 +123,7 @@ export abstract class Resource extends Route implements ResourceInterface {
};

public put = <
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments | null, requestConfig?: RequestConfig | null
Expand All @@ -131,7 +132,7 @@ export abstract class Resource extends Route implements ResourceInterface {
};

public del = <
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments | null, requestConfig?: RequestConfig | null
Expand All @@ -140,7 +141,7 @@ export abstract class Resource extends Route implements ResourceInterface {
};

public renderPath<
Params extends { [K in keyof Params]?: string } = {}
Params extends Kwargs<Params> = {}
>(urlParams: Params | null = null, requestConfig: RequestConfig = null): string {
let thePath = this.apiEndpoint;
const config = this.config(requestConfig);
Expand All @@ -164,7 +165,7 @@ export abstract class Resource extends Route implements ResourceInterface {

protected _fetch<
R = any,
Params extends { [K in keyof Params]?: string } = {}
Params extends Kwargs<Params> = {}
>(
kwargs: Params | null = null, query: Query | null = null, requestConfig: RequestConfig | null = null, method: AllowedFetchMethods
): Promise<R> {
Expand All @@ -175,7 +176,7 @@ export abstract class Resource extends Route implements ResourceInterface {
protected _post<
R = any,
D extends ObjectMap = any,
Params extends { [K in keyof Params]?: string } = {}
Params extends Kwargs<Params> = {}
>(
kwargs: Params | null = null, data: D | string | null = null, query: Query = null, attachments: Attachments = null,
requestConfig: RequestConfig = null, method: AllowedPostMethods
Expand Down
22 changes: 12 additions & 10 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type OptionalMap<T> = {
[K in keyof T]?: T[K]
};

export type Kwargs<KW> = { [K in keyof KW]?: string | undefined; };

export interface ObjectMap<T = any> {
[key: string]: T;
}
Expand Down Expand Up @@ -252,11 +254,11 @@ export interface RouterInterface extends RouteInterface {
}

export type ResourceFetchMethod<
R = any, Params extends { [K in keyof Params]?: string } = {}
R = any, Params extends Kwargs<Params> = {}
> = (kwargs?: Params | null, query?: Query | null, requestConfig?: RequestConfig | null) => Promise<R> | any;

export type ResourcePostMethod<
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
> = (
kwargs?: Params | null, data?: D | string | null, query?: Query | null, attachments?: Attachments, requestConfig?: RequestConfig | null
) => Promise<R> | any;
Expand All @@ -268,42 +270,42 @@ export interface ResourceInterface extends RouteInterface {
config(requestConfig?: RequestConfig): ConfigType;

fetch<
R = any, Params extends { [K in keyof Params]?: string } = {}
R = any, Params extends Kwargs<Params> = {}
>(kwargs?: Params | null, query?: Query | null, requestConfig?: RequestConfig | null): Promise<R> | any;
head<
R = any, Params extends { [K in keyof Params]?: string } = {}
R = any, Params extends Kwargs<Params> = {}
>(kwargs?: Params | null, query?: Query | null, requestConfig?: RequestConfig | null): Promise<R> | any;
options<
R = any, Params extends { [K in keyof Params]?: string } = {}
R = any, Params extends Kwargs<Params> = {}
>(kwargs?: Params | null, query?: Query | null, requestConfig?: RequestConfig | null): Promise<R> | any;

post<
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments, requestConfig?: RequestConfig | null
): Promise<R> | any;
patch<
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments, requestConfig?: RequestConfig | null
): Promise<R> | any;
put<
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments, requestConfig?: RequestConfig | null
): Promise<R> | any;
del<
R = any, D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}
R = any, D extends ObjectMap = any, Params extends Kwargs<Params> = {}
>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments, requestConfig?: RequestConfig | null
): Promise<R> | any;

renderPath<
Params extends { [K in keyof Params]?: string } = {}
Params extends Kwargs<Params> = {}
>(urlParams?: Params | null, requestConfig?: RequestConfig | null): string;

[key: string]: any;
Expand Down
1 change: 1 addition & 0 deletions packages/fetch-runtime/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('cross-fetch/polyfill');
require('abortcontroller-polyfill/dist/abortcontroller-polyfill-only');

if (typeof module !== 'undefined' && module.exports) {
global.FormData = require('form-data');
Expand Down
1 change: 1 addition & 0 deletions packages/fetch-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"polyfill"
],
"dependencies": {
"abortcontroller-polyfill": "^1.2.3",
"cross-fetch": "^3.0.0",
"form-data": "^2.3.3",
"formdata-polyfill": "^3.0.12"
Expand Down
19 changes: 10 additions & 9 deletions packages/saga-router/src/SagaResource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { call } from 'redux-saga/effects';
import {
Attachments,
Kwargs,
ObjectMap,
Query,
RequestConfig,
Expand Down Expand Up @@ -101,53 +102,53 @@ export class SagaResource<Klass extends Resource> extends Resource {
}

/* Public API */
public fetch = <Params extends { [K in keyof Params]?: string } = {}>(
public fetch = <Params extends Kwargs<Params> = {}>(
kwargs?: Params | null, query?: Query | null, sagaRequestConfig?: SagaRequestConfig | null
) => {
return this._sagaFetch<Params | null | undefined>('fetch', kwargs, query, sagaRequestConfig);
};

public head = <Params extends { [K in keyof Params]?: string } = {}>(
public head = <Params extends Kwargs<Params> = {}>(
kwargs?: Params | null, query?: Query | null, sagaRequestConfig?: SagaRequestConfig | null
) => {
return this._sagaFetch<Params | null | undefined>('head', kwargs, query, sagaRequestConfig);
};

public options = <Params extends { [K in keyof Params]?: string } = {}>(
public options = <Params extends Kwargs<Params> = {}>(
kwargs?: Params | null, query?: Query | null, sagaRequestConfig?: SagaRequestConfig | null
) => {
return this._sagaFetch<Params | null | undefined>('options', kwargs, query, sagaRequestConfig);
};

public post = <D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}>(
public post = <D extends ObjectMap = any, Params extends Kwargs<Params> = {}>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments | null, sagaRequestConfig?: SagaRequestConfig | null
) => {
return this._sagaPost<D, Params>('post', kwargs, data, query, attachments, sagaRequestConfig);
};

public patch = <D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}>(
public patch = <D extends ObjectMap = any, Params extends Kwargs<Params> = {}>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments | null, sagaRequestConfig?: SagaRequestConfig | null
) => {
return this._sagaPost<D, Params>('patch', kwargs, data, query, attachments, sagaRequestConfig);
};

public put = <D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}>(
public put = <D extends ObjectMap = any, Params extends Kwargs<Params> = {}>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments | null, sagaRequestConfig?: SagaRequestConfig | null
) => {
return this._sagaPost<D, Params>('put', kwargs, data, query, attachments, sagaRequestConfig);
};

public del = <D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}>(
public del = <D extends ObjectMap = any, Params extends Kwargs<Params> = {}>(
kwargs?: Params | null, data?: D | string | null, query?: Query | null,
attachments?: Attachments | null, sagaRequestConfig?: SagaRequestConfig | null
) => {
return this._sagaPost<D, Params>('del', kwargs, data, query, attachments, sagaRequestConfig);
};

protected _sagaFetch<Params extends { [K in keyof Params]?: string } = {}>(
protected _sagaFetch<Params extends Kwargs<Params> = {}>(
method: ResourceFetchMethods, kwargs: Params | null = null, query: Query | null = null,
sagaRequestConfig: SagaRequestConfig | null = null
) {
Expand All @@ -169,7 +170,7 @@ export class SagaResource<Klass extends Resource> extends Resource {
);
}

protected _sagaPost<D extends ObjectMap = any, Params extends { [K in keyof Params]?: string } = {}>(
protected _sagaPost<D extends ObjectMap = any, Params extends Kwargs<Params> = {}>(
method: ResourcePostMethods, kwargs: Params | null = null, data: D | string | null = null, query: Query | null = null,
attachments: Attachments | null = null, sagaRequestConfig: SagaRequestConfig | null = null
) {
Expand Down
6 changes: 3 additions & 3 deletions packages/saga-router/src/resourceEffectFactory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { call } from 'redux-saga/effects';
import { isFetchMethod, isPostMethod, ObjectMap, RequestConfig, Resource } from 'tg-resources';
import { isFetchMethod, isPostMethod, Kwargs, ObjectMap, RequestConfig, Resource } from 'tg-resources';

import { resourceSagaRunner } from './resourceSagaRunner';
import { isSagaResource, SagaResource } from './SagaResource';
import { ResourceSagaRunnerConfig, SagaRequestConfig } from './types';


export interface EffectCreatorOptions<
Params extends { [K in keyof Params]?: string } = {}, D extends ObjectMap = any
Params extends Kwargs<Params> = {}, D extends ObjectMap = any
> extends ResourceSagaRunnerConfig<Params, D> {
requestConfig?: RequestConfig | SagaRequestConfig | null;
}
Expand All @@ -23,7 +23,7 @@ export interface EffectCreatorOptions<
* @param options Resource method parameters
*/
export const resourceEffectFactory = <
Klass extends Resource, Params extends { [K in keyof Params]?: string } = {}, D extends ObjectMap = any
Klass extends Resource, Params extends Kwargs<Params> = {}, D extends ObjectMap = any
>(
resource: Klass | SagaResource<Klass>,
method: string,
Expand Down
3 changes: 2 additions & 1 deletion packages/saga-router/src/resourceSagaRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { call, cancelled } from 'redux-saga/effects';
import {
isFetchMethod,
isPostMethod,
Kwargs,
ObjectMap,
ResourceInterface,
} from 'tg-resources';
Expand All @@ -13,7 +14,7 @@ import { ResourceSagaRunnerConfig, SagaConfigType } from './types';


export function* resourceSagaRunner<
Params extends { [K in keyof Params]?: string } = {},
Params extends Kwargs<Params> = {},
D extends ObjectMap = any
>(resource: ResourceInterface, method: string, options: ResourceSagaRunnerConfig<Params, D> = {}): SagaIterator {
const {
Expand Down
7 changes: 4 additions & 3 deletions packages/saga-router/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SagaIterator } from '@redux-saga/types';
import {
Attachments,
ConfigType,
Kwargs,
ObjectMap,
Optional,
OptionalMap,
Expand Down Expand Up @@ -30,7 +31,7 @@ export interface SagaConfigType extends SagaConfigTypeBase, ConfigType {
export type SagaRouteConfig = Optional<OptionalMap<SagaRouteConfigType>>;
export type SagaRequestConfig = Optional<OptionalMap<SagaConfigType>>;

export interface ResourceSagaRunnerConfig<Params extends { [K in keyof Params]?: string } = {}, D extends ObjectMap = any> {
export interface ResourceSagaRunnerConfig<Params extends Kwargs<Params> = {}, D extends ObjectMap = any> {
kwargs?: Params | null;
query?: Query | null;
data?: D | string | null;
Expand All @@ -40,11 +41,11 @@ export interface ResourceSagaRunnerConfig<Params extends { [K in keyof Params]?:

export type ErrorType = ResourceErrorInterface | Error;

export interface OnRequestError<Params extends { [K in keyof Params]?: string } = {}, D extends ObjectMap = any> {
export interface OnRequestError<Params extends Kwargs<Params> = {}, D extends ObjectMap = any> {
(error: ErrorType, resource: ResourceInterface, options: ResourceSagaRunnerConfig<Params, D>): void;
(error: ErrorType, resource: ResourceInterface, options: ResourceSagaRunnerConfig<Params, D>): SagaIterator;
}

export type MutatedRequestConfigFn<Params extends { [K in keyof Params]?: string } = {}, D extends ObjectMap = any> = (
export type MutatedRequestConfigFn<Params extends Kwargs<Params> = {}, D extends ObjectMap = any> = (
requestConfig: SagaRequestConfig | undefined, resource: ResourceInterface, config: ResourceSagaRunnerConfig<Params, D>,
) => SagaIterator | SagaRequestConfig | undefined;
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,11 @@ abbrev@1.0.x:
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135"
integrity sha1-kbR5JYinc4wl813W9jdSovh3YTU=

abortcontroller-polyfill@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/abortcontroller-polyfill/-/abortcontroller-polyfill-1.2.3.tgz#d7865594d451ee66f4c0c54ce0a94f8f4f55e563"
integrity sha512-+PU8syN+yBr6h05abhOGazfvPC54u7ur1Aw/s5Y/PKdBY4vT1gGcRwktqZyxHb9+J64mzjERxrxeP++EMxSxnA==

accepts@~1.3.5:
version "1.3.5"
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.5.tgz#eb777df6011723a3b14e8a72c0805c8e86746bd2"
Expand Down

0 comments on commit db2d053

Please sign in to comment.