You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-16Lines changed: 29 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,10 @@ Simple, customizable, offline-first API wrapper for react-native.
4
4
5
5
Easily write offline-first react-native applications with your own REST API. This module supports every major features for network requests : middlewares, fine-grained control over caching logic, custom caching driver...
In this short example, we're firing a `GET` request on the path `http://staging.myapi.tld/api/v2/documents/:xSfdk21`. If you don't understand how this path is constructed, see [path and query parameters](#path-and-query-parameters).
125
+
In this short example, we're firing a `GET` request on the path `http://staging.myapi.tld/api/v2/documents/xSfdk21`. If you don't understand how this path is constructed, see [path and query parameters](#path-and-query-parameters).
122
126
123
127
A couple of notes :
124
128
@@ -130,7 +134,7 @@ A couple of notes :
130
134
Name | Description | Parameters | Return value
131
135
------ | ------ | ------ | ------
132
136
`fetch` | Fires a network request to one of your service with additional options, see [fetch options](#fetch-options) | `service: string, options?: IFetchOptions` | `Promise<any>`
133
-
`fetchHeaders` | Just like `fetch` but only returns HTTP headers of the reponse | `service: string, options?: IFetchOptions` | `Promise<any>`
137
+
`fetchHeaders` | Just like `fetch` but only returns the HTTP headers of the reponse | `service: string, options?: IFetchOptions` | `Promise<any>`
134
138
`clearCache` | Clears all the cache, or just the one of a specific service | `service?: string` | `Promise<void>`
135
139
`setOptions` | Sets or update the API options of the wrapper | `options: IAPIOptions` | `void`
136
140
`setServices` | Sets or update your services definitions | `services: IAPIServices` | `void`
@@ -144,12 +148,12 @@ Key | Type | Description | Example
144
148
------ | ------ | ------ | ------
145
149
`domains` | `{ default: string, [key: string]: string }` | **Required**, full URL to your domains | `domains: {default: 'http://myapi.tld', staging: 'http://staging.myapi.tld' },`
146
150
`prefixes` | `{ default: string, [key: string]: string }` | **Required**, prefixes your API uses, `default` is required, leave it blank if you don't have any | `{ default: '/api/v1', apiV2: '/api/v2' }`
147
-
`middlewares` | Optional array of `APIMiddleware`, see [middlewares](#middlewares) | `[authFunc, trackFunc]`
`printNetworkRequests` | `boolean` | Optional, prints all your network requests
150
154
`disableCache` | `boolean` | Optional, completely disables caching (overriden by service definitions & `fetch`'s `option` parameter)
151
155
`cacheExpiration` | `number` | Optional default expiration of cached data in ms (overriden by service definitions & `fetch`'s `option` parameter)
152
-
`offlineDriver` | `IAPIDriver` | Optional, See[use your own driver for caching](#use-your-own-driver-for-caching)
156
+
`offlineDriver` | `IAPIDriver` | Optional, see[use your own driver for caching](#use-your-own-driver-for-caching)
153
157
154
158
## Services options
155
159
@@ -159,28 +163,28 @@ Key | Type | Description | Example
159
163
------ | ------ | ------ | ------
160
164
`path` | `string` | Required path to your endpoint, see [path and query parameters](#path-and-query-parameters) | `article/:articleId`
161
165
`expiration` | `number` | Optional time in ms before this service's cached data becomes stale, defaults to 5 minutes
162
-
`method` | HTTP Methods | Optional HTTP method of your request, defaults to `GET`
166
+
`method` | `'GET' | 'POST' | 'OPTIONS'...` | Optional HTTP method of your request, defaults to `GET`
163
167
`domain` | `string` | Optional specific domain to use for this service, provide the key you set in your `domains` API option
164
168
`prefix` | `string` | Optional specific prefix to use for this service, provide the key you set in your `prefixes` API option
165
-
`middlewares` | `APIMiddleware[]` | Optional array of middlewares that override the ones set globally in your `middlewares` API option
166
-
`disableCache` | `boolean` | Optional, disables the cache for this service that override your API option
169
+
`middlewares` | `APIMiddleware[]` | Optional array of middlewares that override the ones set globally in your `middlewares` API option, , see [middlewares](#middlewares)
170
+
`disableCache` | `boolean` | Optional, disables the cache for this service (override your [API's global options](#api-options))
167
171
168
172
## Fetch options
169
173
170
-
The `options` parameter of the `fetch` methods overrides the configuration you set globally in your API options, and the one you set for your endpoints. This is a good way of making very specific calls without having to declare another service for a single use for instance.
174
+
The `options` parameter of the `fetch` and `fetchHeaders` method overrides the configuration you set globally in your API options, and the one you set for your services definitions. For instance, this is a good way of making very specific calls without having to declare another service just to tweak a single option.
175
+
176
+
Important notes :
171
177
172
-
All of these are optional.
178
+
* All of these are optional.
179
+
* All the keys of [services options](#services-options) can be overriden here ! You could disable caching just for a single call for example, but still having it enabled in your service's definition.
173
180
174
181
Key | Type | Description | Example
175
182
------ | ------ | ------ | ------
176
183
`pathParameters` | `{ [key: string]: string }` | Parameters to replace in your path, see [path and query parameters](#path-and-query-parameters) | `{ documentId: 'xSfdk21' }`
177
184
`queryParameters` | `{ [key: string]: string }` | Query parameters that will be appended to your service's path, , see [path and query parameters](#path-and-query-parameters) | `{ refresh: true, orderBy: 'date' }`
178
185
`headers` | `{ [key: string]: string }` | HTTP headers you need to pass in your request
179
-
`middlewares` | `APIMiddleware[]` | Optional array of middlewares that override the ones set globally in your `middlewares` API option and in your service's definition
180
-
`fetchOptions` | `any` | Optional, any value passed here will be merged into the options of react-native's `fetch method` so you'll be able to configure anything not provided by the wrapper itself
181
-
182
-
183
-
**All the keys of [service options](#service-options) can be overriden here !** You could disable caching just for a single call for example, but still having it enabled in your service's definition.
186
+
`middlewares` | `APIMiddleware[]` | Optional array of middlewares that override the ones set globally in your `middlewares` API option and in your service's definition, , see [middlewares](#middlewares)
187
+
`fetchOptions` | `any` | Optional, any value passed here will be merged into the options of react-native's `fetch` method so you'll be able to configure anything not provided by the wrapper itself
184
188
185
189
## Path and query parameters
186
190
@@ -196,7 +200,7 @@ Just like for the other request options, **you can provide middlewares at the gl
196
200
197
201
You must provide an **array of promises**, like so : `(serviceDefinition: IAPIService, options: IFetchOptions) => any;`, please [take a look at the types](#types) to know more. You don't necessarily need to write asynchronous code in them, but they all must be promises.
198
202
199
-
Anything you will resolved in those promises will be merged into your request's options !
203
+
Anything you will resolve in those promises will be merged into your request's options !
// This will be printed everytime you call a service
210
215
console.log('You just fired a request for the path '+serviceDefinition.path);
211
216
}
212
217
```
@@ -261,4 +266,12 @@ Your custom driver must implement these 3 methods that are promises.
261
266
262
267
Every API interfaces [can be seen here](src/interfaces.ts) so you don't need to poke around the parameters in your console to be aware of what's available to you :)
263
268
264
-
These are Typescript defintions, so they should be displayed in your editor/IDE if it supports it.
269
+
These are Typescript defintions, so they should be displayed in your editor/IDE if it supports it.
270
+
271
+
## Roadmap
272
+
273
+
Pull requests are more than welcome for these items, or for any feature that might be missing.
274
+
275
+
-[ ] Write a demo
276
+
-[ ] Thoroughly test custom caching drivers, maybe provide one (realm or sqlite)
0 commit comments