0.2.1 • Published 4 years ago

@galaxis/fetch v0.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Galaxis Fetch

npm

A Galaxis network interface that uses Fetch API, extended with types and custom body data.

Installation

yarn add @galaxis/fetch

You need to install Galaxis Core as well, directly or indirectly.

The library is compiled to modern JS, but it should work in all reasonable browsers with the help of properly configured Babel.

Public API

⚠ Anything that is not documented here is not considered a part of public API and may change at any time.

getRequestFactory()

Returns a getRequestFactory function that can be used in query or mutation.

const query: AppQuery = {
    requestParams: { foo: 'foo' },
    getRequestFactory: getRequestFactory({ processResponse, fetch }),
};

Arguments

GetRequestFactoryOptions
NameTypeDescriptionRequired
processResponse(response: Response) => DA function that takes Response and returns request data or throws an error.Yes
fetchtypeof fetchfetch function to use instead of built-in fetch.No

Return value

(opts: RequestOptions) => (abortSignal?: AbortSignal) => Promise<D>;

getRequestId()

Returns a getRequestId function that can be used in query or mutation. Generates request id in the form of [url]:[request-params-hash]. Excludes server-specific parts of requestParams.

const query: AppQuery = {
    requestParams: { foo: 'foo' },
    getRequestId: getRequestId({ hash }),
};

Arguments

GetRequestIdOptions
NameTypeDescriptionRequired
hash(value: unknown) => stringA function that takes hash from requestParams.Yes

Return value

(opts: RequestOptions) => string;

processResponseJson()

This function can be used as processResponse parameter of getRequestFactory().

It expects the Response to be in JSON format. If response code is not in 200-299 range, it throws a ResponseError.

CustomData

This is an abstract class for representing formats of data that are not natively supported by Fetch API. A CustomData instance has the following fields:

NameTypeDescription
dataTSome data.
contentTypestringValue for the Content-Type header.
serialize() => stringA function that serializes internal data field to string.

JsonData

This class implements CustomData and allows usage of JSON.

const jsonData = new JsonData({ foo: 'foo' });

ResponseError

This error should be thrown in processResponse function of getRequestFactory().

throw new ResponseError(response, code, message);

Arguments

NameTypeDescriptionRequired
responseTSome data associated with the response, usually response body.Yes
codenumberResponse code.Yes
messagestringHuman-readable error message for development purposes.No

Return value

Extends Error.

NameTypeDescription
namestringAlways has a value of 'ResponseError'.
codenumberResponse code.
responseTSome data associated with the response, usually response body.

Important types

Constraints

NameTypeDescription
PathConstraintRecord<string, string | number>Path parameters.
QueryConstraintStringifiableRecordQuery parameters.
HeadersConstraintHeadersInitHeaders.
BodyConstraintCustomData | BodyInit | nullBody.

RequestParamsConstraint

NameTypeRequired
pathParamsPathConstraintNo
queryParamsQueryConstraintNo
headersHeadersConstraintNo
bodyBodyConstraintNo

DynamicRequestParams

It's a generic that takes the given type T, which is constrained by RequestParamsConstraint, and adds fields from RequestInit, omitting body and headers. RequestInit is from Fetch API.

It describes the part of requestParams that is dynamic for the given resource. Note how all fields are optional by default.

export type DynamicParams<T extends ParamsConstraint = ParamsConstraint> = T & Omit<RequestInit, 'body' | 'headers'>;

GlobalRequestParams

This type describes requestParams that can be specified globally, for all resources. Note how all fields are optional by default.

Extends DynamicRequestParams.

NameTypeDescriptionRequired
rootstringUrl part that is common for all resources, e.g. 'https://domain.com/api'. May be different between client and server.No

RequestParams

This type describes requestParams for a specific resource. Note how all fields are optional by default, except path.

Extends GlobalRequestParams.

NameTypeDescriptionRequired
pathstringPath to the given resource, e.g. '/entity/:id'. It is processed by path-to-regexp.Yes