Skip to content

Commit

Permalink
feat: deprecate useJsonApiFetch as this is not really a React hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Strobotti committed Jun 2, 2023
1 parent 26a05df commit 0d80d31
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ npm i jsonapi-ts-fetch
## Usage

```typescript
import { Fetch, getDeserializer, ItemDeserializer, RelationshipDeserializer, GetParams } from 'jsonapi-ts-fetch';
import { getJsonApiFetch, Fetch, getDeserializer, ItemDeserializer, RelationshipDeserializer, GetParams } from 'jsonapi-ts-fetch';

// Introduce types for your entities, the folder:
type Folder = {
Expand Down Expand Up @@ -69,7 +69,7 @@ const deserializer = getDeserializer([
]);

// create a fetcher:
export default function useFetch(): Fetch {
const myFetch = (): Fetch => {
return {
get: request('GET'),
post: request('POST'),
Expand Down Expand Up @@ -101,7 +101,7 @@ export default function useFetch(): Fetch {
}

// use the JsonapiFetcher:
const jsonApiFetch: JsonApiFetch<Folder> = useJsonApiFetch(fetch, '/api/v1/folders', deserializer);
const jsonApiFetch: JsonApiFetch<Folder> = getJsonApiFetch(myFetch, '/api/v1/folders', deserializer);

// Filter by your heart's content:
const params: GetParams = {
Expand Down
12 changes: 6 additions & 6 deletions src/__tests__/JsonApiFetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ItemDeserializer,
RelationshipDeserializer,
} from 'jsonapi-ts-deserializer';
import useJsonApiFetch, { Fetch, JsonApiFetch, JsonApiResponse } from '../index';
import { Fetch, getJsonApiFetch, JsonApiFetch, JsonApiResponse } from '../index';

const jsonapiOrgExampleData = {
links: {
Expand Down Expand Up @@ -436,7 +436,7 @@ describe('JsonApiFetch', () => {
const deserializer: Deserializer = getDeserializer([articleDeserializer, personDeserializer, commentDeserializer]);
const fetch = new FetchMock(new Response(JSON.stringify(jsonapiOrgExampleData)));

const jsonApiFetch: JsonApiFetch<Article> = useJsonApiFetch(fetch, '/does/not/matter', deserializer);
const jsonApiFetch: JsonApiFetch<Article> = getJsonApiFetch(fetch, '/does/not/matter', deserializer);

jsonApiFetch.find({}, ['author', 'comments']).then((response: JsonApiResponse<Article>) => {
expect(response).toMatchSnapshot();
Expand All @@ -447,7 +447,7 @@ describe('JsonApiFetch', () => {
const deserializer: Deserializer = getDeserializer([articleDeserializer, personDeserializer, commentDeserializer]);
const fetch = new FetchMock(new Response(JSON.stringify(jsonapiOrgExampleData2)));

const jsonApiFetch: JsonApiFetch<Article> = useJsonApiFetch(fetch, '/does/not/matter', deserializer);
const jsonApiFetch: JsonApiFetch<Article> = getJsonApiFetch(fetch, '/does/not/matter', deserializer);

jsonApiFetch.find({}, []).then((response: JsonApiResponse<Article>) => {
expect(response).toMatchSnapshot();
Expand All @@ -458,7 +458,7 @@ describe('JsonApiFetch', () => {
const deserializer: Deserializer = getDeserializer([folderDeserializer, fileDeserializer]);
const fetch = new FetchMock(new Response(JSON.stringify(fileSystemExampleData)));

const jsonApiFetch: JsonApiFetch<Folder> = useJsonApiFetch(fetch, '/does/not/matter', deserializer);
const jsonApiFetch: JsonApiFetch<Folder> = getJsonApiFetch(fetch, '/does/not/matter', deserializer);

jsonApiFetch.find({}, ['children']).then((response: JsonApiResponse<Folder>) => {
expect(response).toMatchSnapshot();
Expand All @@ -469,7 +469,7 @@ describe('JsonApiFetch', () => {
const deserializer: Deserializer = getDeserializer([folderDeserializer, fileDeserializer]);
const fetch = new FetchMock(new Response(JSON.stringify(fileSystemExampleData2)));

const jsonApiFetch: JsonApiFetch<Folder> = useJsonApiFetch(fetch, '/does/not/matter', deserializer);
const jsonApiFetch: JsonApiFetch<Folder> = getJsonApiFetch(fetch, '/does/not/matter', deserializer);

jsonApiFetch.find({}, ['children']).then((response: JsonApiResponse<Folder>) => {
expect(response).toMatchSnapshot();
Expand All @@ -480,7 +480,7 @@ describe('JsonApiFetch', () => {
const deserializer: Deserializer = getDeserializer([folderDeserializer, fileDeserializer]);
const fetch = new FetchMock(new Response(JSON.stringify(fileSystemExampleData3)));

const jsonApiFetch: JsonApiFetch<Folder> = useJsonApiFetch(fetch, '/does/not/matter', deserializer);
const jsonApiFetch: JsonApiFetch<Folder> = getJsonApiFetch(fetch, '/does/not/matter', deserializer);

jsonApiFetch.findOne('1', []).then((response: JsonApiResponse<Folder>) => {
expect(response).toMatchSnapshot();
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export type EntitySerializer<T> = {
};

/**
* A generic service for interacting with a JSON:API endpoint
* Instantiate a generic service for interacting with a JSON:API endpoint
*
* @param fetch
* @param route
* @param deserializer
* @param serializer
*/
const useJsonApiFetch = <T>(
export const getJsonApiFetch = <T>(
fetch: Fetch,
route: string,
deserializer: Deserializer,
Expand Down Expand Up @@ -187,4 +187,7 @@ const useJsonApiFetch = <T>(
};
};

export default useJsonApiFetch;
/**
* @deprecated Use getJsonApiFetch instead: this is not really a React hook so lets not call it that
*/
export default getJsonApiFetch;

0 comments on commit 0d80d31

Please sign in to comment.