Skip to content

Commit 34e2e51

Browse files
authored
enhance: createResource() -> resource() (#3158)
1 parent 8003ebb commit 34e2e51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+329
-317
lines changed

.changeset/silent-bananas-relax.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@data-client/rest': patch
3+
---
4+
5+
createResource() -> [resource()](https://dataclient.io/rest/api/resource)
6+
7+
Note: `createResource` is still exported (it is the same)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ class Article extends Entity {
7070
### Create [collection of API Endpoints](https://dataclient.io/docs/getting-started/resource)
7171

7272
```typescript
73-
const UserResource = createResource({
73+
const UserResource = resource({
7474
path: '/users/:id',
7575
schema: User,
7676
optimistic: true,
7777
});
7878

79-
const ArticleResource = createResource({
79+
const ArticleResource = resource({
8080
path: '/articles/:id',
8181
schema: Article,
8282
searchParams: {} as { author?: string },
@@ -276,7 +276,7 @@ For the small price of 9kb gziped.    [🏁Get started now](https://da
276276

277277
- Networking definition
278278
- [Endpoints](https://dataclient.io/rest/api/Endpoint): [RestEndpoint](https://dataclient.io/rest/api/RestEndpoint), [GQLEndpoint](https://dataclient.io/graphql/api/GQLEndpoint)
279-
- [Resources](https://dataclient.io/docs/getting-started/resource): [createResource()](https://dataclient.io/rest/api/createResource), [hookifyResource()](https://dataclient.io/rest/api/hookifyResource)
279+
- [Resources](https://dataclient.io/docs/getting-started/resource): [resource()](https://dataclient.io/rest/api/resource), [hookifyResource()](https://dataclient.io/rest/api/hookifyResource)
280280
- [Data model](https://dataclient.io/docs/concepts/normalization)
281281
- [Entity](https://dataclient.io/rest/api/Entity), [schema.Entity](https://dataclient.io/rest/api/schema.Entity) mixin, [GQLEntity](https://dataclient.io/graphql/api/GQLEntity)
282282
- [Object](https://dataclient.io/rest/api/Object)

__tests__/new.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
schema,
33
Endpoint,
4-
createResource,
4+
resource,
55
RestEndpoint,
66
Schema,
77
Entity,
@@ -78,7 +78,7 @@ export class VisEndpoint<O extends RestGenerics = any> extends RestEndpoint<O> {
7878
return super.getRequestInit(body);
7979
}
8080
}
81-
const VisSettingsResourceBase = createResource({
81+
const VisSettingsResourceBase = resource({
8282
path: 'http\\://test.com/vis-settings/:id',
8383
schema: VisSettings,
8484
Endpoint: VisEndpoint,
@@ -114,7 +114,7 @@ export const VisSettingsResource = {
114114
},
115115
}),
116116
};
117-
const VisSettingsResourceBaseFromMixin = createResource({
117+
const VisSettingsResourceBaseFromMixin = resource({
118118
path: 'http\\://test.com/vis-settings/:id',
119119
schema: VisSettingsFromMixin,
120120
Endpoint: VisEndpoint,
@@ -167,7 +167,7 @@ export class User extends Entity {
167167
return this.id?.toString();
168168
}
169169
}
170-
export const UserResource = createResource({
170+
export const UserResource = resource({
171171
path: 'http\\://test.com/user/:id',
172172
schema: User,
173173
});
@@ -207,15 +207,15 @@ export class ArticleFromMixin extends schema.Entity(ArticleData, {
207207
class ArticleEndpoint<O extends RestGenerics = any> extends RestEndpoint<O> {}
208208

209209
interface ArticleGenerics {
210-
/** @see https://dataclient.io/rest/api/createResource#path */
210+
/** @see https://dataclient.io/rest/api/resource#path */
211211
readonly path?: string;
212-
/** @see https://dataclient.io/rest/api/createResource#schema */
212+
/** @see https://dataclient.io/rest/api/resource#schema */
213213
readonly schema: Schema;
214214
/** Only used for types */
215-
/** @see https://dataclient.io/rest/api/createResource#body */
215+
/** @see https://dataclient.io/rest/api/resource#body */
216216
readonly body?: any;
217217
/** Only used for types */
218-
/** @see https://dataclient.io/rest/api/createResource#searchParams */
218+
/** @see https://dataclient.io/rest/api/resource#searchParams */
219219
readonly searchParams?: any;
220220
}
221221
function createArticleResource<O extends ArticleGenerics>({
@@ -237,7 +237,7 @@ function createArticleResource<O extends ArticleGenerics>({
237237
> extends Endpoint<O> {
238238
urlPrefix = `http://test.com/${urlRoot}`;
239239
}
240-
const resource = createResource({
240+
const BaseResource = resource({
241241
path: '/:id',
242242
schema,
243243
Endpoint: EndpointUrlRootOverride,
@@ -256,7 +256,7 @@ function createArticleResource<O extends ArticleGenerics>({
256256
}),
257257
}));
258258
if (!optimistic) {
259-
return (resource as any).extend({
259+
return (BaseResource as any).extend({
260260
partialUpdate: {
261261
getOptimisticResponse: (snap, params, body) => ({
262262
id: params.id,
@@ -268,7 +268,7 @@ function createArticleResource<O extends ArticleGenerics>({
268268
},
269269
});
270270
}
271-
return resource as any;
271+
return BaseResource as any;
272272
}
273273
export const ArticleResource = createArticleResource({ schema: Article });
274274
export const ArticleSlugResource = createArticleResource({
@@ -277,7 +277,7 @@ export const ArticleSlugResource = createArticleResource({
277277

278278
export const AuthContext = createContext('');
279279

280-
const ContextAuthdArticleResourceBase = createResource({
280+
const ContextAuthdArticleResourceBase = resource({
281281
path: 'http\\://test.com/article/:id',
282282
schema: Article,
283283
});
@@ -527,7 +527,7 @@ export const CoolerArticleDetail = new Endpoint(
527527
export class IndexedUser extends User {
528528
static readonly indexes = ['username'];
529529
}
530-
export const IndexedUserResource = createResource({
530+
export const IndexedUserResource = resource({
531531
path: 'http\\://test.com/user/:id',
532532
schema: IndexedUser,
533533
});
@@ -642,7 +642,7 @@ export const UnionSchema = new schema.Union(
642642
},
643643
'type',
644644
);
645-
const UnionResourceBase = createResource({
645+
const UnionResourceBase = resource({
646646
path: '/union/:id',
647647
schema: UnionSchema,
648648
});

docs/core/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ At this point we've defined `todoDetail`, `todoList` and `todoUpdate`. You might
336336
that these endpoint definitions share some logic and information. For this reason Reactive Data Client
337337
encourages extracting shared logic among endpoints.
338338

339-
[Resources](/rest/api/createResource) are collections of endpoints that operate on the same data.
339+
[Resources](/rest/api/resource) are collections of endpoints that operate on the same data.
340340

341341
```typescript
342-
import { Entity, createResource } from '@data-client/rest';
342+
import { Entity, resource } from '@data-client/rest';
343343

344344
class Todo extends Entity {
345345
id = 0;
@@ -352,7 +352,7 @@ class Todo extends Entity {
352352
}
353353
}
354354

355-
const TodoResource = createResource({
355+
const TodoResource = resource({
356356
urlPrefix: 'https://jsonplaceholder.typicode.com',
357357
path: '/todos/:id',
358358
schema: Todo,

docs/core/api/Controller.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ To refresh while continuing to display stale data - [Controller.fetch](#fetch).
257257

258258
Use [schema.Invalidate](/rest/api/Invalidate) to invalidate every endpoint that contains a given entity.
259259

260-
For REST try using [Resource.delete](/rest/api/createResource#delete)
260+
For REST try using [Resource.delete](/rest/api/resource#delete)
261261

262262
```ts
263263
// deletes MyResource(5)

docs/core/api/useCache.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ delay: 500,
3131
]} row>
3232

3333
```ts title="UserResource" collapsed
34-
import { Entity, createResource } from '@data-client/rest';
34+
import { Entity, resource } from '@data-client/rest';
3535

3636
export class User extends Entity {
3737
id = '';
@@ -42,7 +42,7 @@ export class User extends Entity {
4242
}
4343
static key = 'User';
4444
}
45-
export const UserResource = createResource({
45+
export const UserResource = resource({
4646
path: '/users/:id',
4747
schema: User,
4848
}).extend('current', {
@@ -115,7 +115,7 @@ more information about type handling
115115

116116
| Expiry Status | Returns | Conditions |
117117
| ------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
118-
| Invalid | `undefined` | not in store, [deletion](/rest/api/createResource#delete), [invalidation](./Controller.md#invalidate), [invalidIfStale](../concepts/expiry-policy.md#endpointinvalidifstale) |
118+
| Invalid | `undefined` | not in store, [deletion](/rest/api/resource#delete), [invalidation](./Controller.md#invalidate), [invalidIfStale](../concepts/expiry-policy.md#endpointinvalidifstale) |
119119
| Stale | denormalized | (first-render, arg change) & [expiry &lt; now](../concepts/expiry-policy.md) |
120120
| Valid | denormalized | fetch completion |
121121
| | `undefined` | `null` used as second argument |

docs/core/api/useDLE.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ In case you cannot use [suspense](../getting-started/data-dependency.md#async-fa
2626
<HooksPlayground fixtures={listFixtures} row>
2727

2828
```typescript title="ProfileResource" collapsed
29-
import { Entity, createResource } from '@data-client/rest';
29+
import { Entity, resource } from '@data-client/rest';
3030

3131
export class Profile extends Entity {
3232
id: number | undefined = undefined;
@@ -40,7 +40,7 @@ export class Profile extends Entity {
4040
static key = 'Profile';
4141
}
4242

43-
export const ProfileResource = createResource({
43+
export const ProfileResource = resource({
4444
path: '/profiles/:id',
4545
schema: Profile,
4646
});
@@ -77,7 +77,7 @@ render(<ProfileList />);
7777

7878
| Expiry Status | Fetch | Data | Loading | Error | Conditions |
7979
| ------------- | --------------- | ------------ | ------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
80-
| Invalid | yes<sup>1</sup> | `undefined` | true | false | not in store, [deletion](/rest/api/createResource#delete), [invalidation](./Controller.md#invalidate), [invalidIfStale](../concepts/expiry-policy.md#endpointinvalidifstale) |
80+
| Invalid | yes<sup>1</sup> | `undefined` | true | false | not in store, [deletion](/rest/api/resource#delete), [invalidation](./Controller.md#invalidate), [invalidIfStale](../concepts/expiry-policy.md#endpointinvalidifstale) |
8181
| Stale | yes<sup>1</sup> | denormalized | false | false | (first-render, arg change) & [expiry &lt; now](../concepts/expiry-policy.md) |
8282
| Valid | no | denormalized | false | maybe<sup>2</sup> | fetch completion |
8383
| | no | `undefined` | false | false | `null` used as second argument |
@@ -140,7 +140,7 @@ function useDLE<
140140
<HooksPlayground fixtures={detailFixtures} row>
141141

142142
```typescript title="ProfileResource" collapsed
143-
import { Entity, createResource } from '@data-client/rest';
143+
import { Entity, resource } from '@data-client/rest';
144144

145145
export class Profile extends Entity {
146146
id: number | undefined = undefined;
@@ -154,7 +154,7 @@ export class Profile extends Entity {
154154
static key = 'Profile';
155155
}
156156

157-
export const ProfileResource = createResource({
157+
export const ProfileResource = resource({
158158
path: '/profiles/:id',
159159
schema: Profile,
160160
});
@@ -194,7 +194,7 @@ render(<ProfileDetail />);
194194
<TypeScriptEditor row={false}>
195195

196196
```ts title="Resources" collapsed
197-
import { Entity, createResource } from '@data-client/rest';
197+
import { Entity, resource } from '@data-client/rest';
198198

199199
export class Post extends Entity {
200200
id = 0;
@@ -207,7 +207,7 @@ export class Post extends Entity {
207207
}
208208
static key = 'Post';
209209
}
210-
export const PostResource = createResource({
210+
export const PostResource = resource({
211211
path: '/posts/:id',
212212
schema: Post,
213213
});
@@ -229,7 +229,7 @@ export class User extends Entity {
229229
}
230230
static key = 'User';
231231
}
232-
export const UserResource = createResource({
232+
export const UserResource = resource({
233233
urlPrefix: 'https://jsonplaceholder.typicode.com',
234234
path: '/users/:id',
235235
schema: User,

docs/core/api/useFetch.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function MasterPost({ id }: { id: number }) {
3737

3838
| Expiry Status | Fetch | Returns | Conditions |
3939
| ------------- | --------------- | ----------- | ----------------------------------------------------------------------------------------------------- |
40-
| Invalid | yes<sup>1</sup> | Promise | not in store, [deletion](/rest/api/createResource#delete), [invalidation](./Controller.md#invalidate) |
40+
| Invalid | yes<sup>1</sup> | Promise | not in store, [deletion](/rest/api/resource#delete), [invalidation](./Controller.md#invalidate) |
4141
| Stale | yes<sup>1</sup> | Promise | (first-render, arg change) & [expiry &lt; now](../concepts/expiry-policy.md) |
4242
| Valid | no | `undefined` | fetch completion |
4343
| | no | `undefined` | `null` used as second argument |

docs/core/api/useQuery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class User extends Entity {
100100
}
101101
static key = 'User';
102102
}
103-
export const UserResource = createResource({
103+
export const UserResource = resource({
104104
path: '/users/:id',
105105
schema: User,
106106
});

docs/core/api/useSuspense.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ values={[
4444
<HooksPlayground fixtures={detailFixtures} row>
4545

4646
```typescript title="ProfileResource" collapsed
47-
import { Entity, createResource } from '@data-client/rest';
47+
import { Entity, resource } from '@data-client/rest';
4848

4949
export class Profile extends Entity {
5050
id: number | undefined = undefined;
@@ -58,7 +58,7 @@ export class Profile extends Entity {
5858
static key = 'Profile';
5959
}
6060

61-
export const ProfileResource = createResource({
61+
export const ProfileResource = resource({
6262
path: '/profiles/:id',
6363
schema: Profile,
6464
});
@@ -139,7 +139,7 @@ Cache policy is [Stale-While-Revalidate](https://tools.ietf.org/html/rfc5861) by
139139

140140
| Expiry Status | Fetch | Suspend | Error | Conditions |
141141
| ------------- | --------------- | ------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
142-
| Invalid | yes<sup>1</sup> | yes | no | not in store, [deletion](/rest/api/createResource#delete), [invalidation](./Controller.md#invalidate), [invalidIfStale](../concepts/expiry-policy.md#endpointinvalidifstale) |
142+
| Invalid | yes<sup>1</sup> | yes | no | not in store, [deletion](/rest/api/resource#delete), [invalidation](./Controller.md#invalidate), [invalidIfStale](../concepts/expiry-policy.md#endpointinvalidifstale) |
143143
| Stale | yes<sup>1</sup> | no | no | (first-render, arg change) & [expiry &lt; now](../concepts/expiry-policy.md) |
144144
| Valid | no | no | maybe<sup>2</sup> | fetch completion |
145145
| | no | no | no | `null` used as second argument |
@@ -196,7 +196,7 @@ function useSuspense<
196196
<HooksPlayground fixtures={listFixtures} row>
197197

198198
```typescript title="ProfileResource" collapsed
199-
import { Entity, createResource } from '@data-client/rest';
199+
import { Entity, resource } from '@data-client/rest';
200200

201201
export class Profile extends Entity {
202202
id: number | undefined = undefined;
@@ -210,7 +210,7 @@ export class Profile extends Entity {
210210
static key = 'Profile';
211211
}
212212

213-
export const ProfileResource = createResource({
213+
export const ProfileResource = resource({
214214
path: '/profiles/:id',
215215
schema: Profile,
216216
});
@@ -268,7 +268,7 @@ function PostWithAuthor() {
268268
<TypeScriptEditor row={false}>
269269

270270
```ts title="Resources" collapsed
271-
import { Entity, createResource } from '@data-client/rest';
271+
import { Entity, resource } from '@data-client/rest';
272272

273273
export class Post extends Entity {
274274
id = 0;
@@ -281,7 +281,7 @@ export class Post extends Entity {
281281
}
282282
static key = 'Post';
283283
}
284-
export const PostResource = createResource({
284+
export const PostResource = resource({
285285
path: '/posts/:id',
286286
schema: Post,
287287
});
@@ -303,7 +303,7 @@ export class User extends Entity {
303303
}
304304
static key = 'User';
305305
}
306-
export const UserResource = createResource({
306+
export const UserResource = resource({
307307
urlPrefix: 'https://jsonplaceholder.typicode.com',
308308
path: '/users/:id',
309309
schema: User,

0 commit comments

Comments
 (0)