@galaxis/fetch v0.2.1
Galaxis Fetch
A Galaxis network interface that uses Fetch API, extended with types and custom body data.
Installation
yarn add @galaxis/fetchYou 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
| Name | Type | Description | Required |
|---|---|---|---|
| processResponse | (response: Response) => D | A function that takes Response and returns request data or throws an error. | Yes |
| fetch | typeof fetch | fetch 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
| Name | Type | Description | Required |
|---|---|---|---|
| hash | (value: unknown) => string | A 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:
| Name | Type | Description |
|---|---|---|
| data | T | Some data. |
| contentType | string | Value for the Content-Type header. |
| serialize | () => string | A 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
| Name | Type | Description | Required |
|---|---|---|---|
| response | T | Some data associated with the response, usually response body. | Yes |
| code | number | Response code. | Yes |
| message | string | Human-readable error message for development purposes. | No |
Return value
Extends Error.
| Name | Type | Description |
|---|---|---|
| name | string | Always has a value of 'ResponseError'. |
| code | number | Response code. |
| response | T | Some data associated with the response, usually response body. |
Important types
Constraints
| Name | Type | Description |
|---|---|---|
| PathConstraint | Record<string, string | number> | Path parameters. |
| QueryConstraint | StringifiableRecord | Query parameters. |
| HeadersConstraint | HeadersInit | Headers. |
| BodyConstraint | CustomData | BodyInit | null | Body. |
RequestParamsConstraint
| Name | Type | Required |
|---|---|---|
| pathParams | PathConstraint | No |
| queryParams | QueryConstraint | No |
| headers | HeadersConstraint | No |
| body | BodyConstraint | No |
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.
| Name | Type | Description | Required |
|---|---|---|---|
| root | string | Url 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.
| Name | Type | Description | Required |
|---|---|---|---|
| path | string | Path to the given resource, e.g. '/entity/:id'. It is processed by path-to-regexp. | Yes |