Skip to content

Commit

Permalink
enhance: Keep legacy Resource (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jul 27, 2020
1 parent 5e5c125 commit fdd1f7c
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 169 deletions.
9 changes: 2 additions & 7 deletions __tests__/common.ts
@@ -1,16 +1,11 @@
import {
Resource,
SchemaList,
schemas,
ReadShape,
SchemaDetail,
} from 'rest-hooks';
import { SchemaList, schemas, ReadShape, SchemaDetail } from 'rest-hooks';
import {
AbstractInstanceType,
MutateShape,
SimpleRecord,
} from '@rest-hooks/core';
import { Endpoint, EndpointExtraOptions } from '@rest-hooks/endpoint';
import { Resource } from '@rest-hooks/rest';
import React from 'react';

export class UserResource extends Resource {
Expand Down
1 change: 1 addition & 0 deletions __tests__/tsconfig.json
Expand Up @@ -6,6 +6,7 @@
"include": ["."],
"references": [
{ "path": "../packages/endpoint" },
{ "path": "../packages/rest" },
{ "path": "../packages/core" },
{ "path": "../packages/rest-hooks" }
]
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Expand Up @@ -51,6 +51,7 @@ const baseConfig = {
const packages = [
'legacy',
'endpoint',
'rest',
'core',
'rest-hooks',
'normalizr',
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-hooks/src/index.ts
Expand Up @@ -70,7 +70,7 @@ export type {
} from '@rest-hooks/core';

export { Resource, SimpleResource } from './resource';
export type { SchemaDetail, SchemaList } from './resource/types';
export type { SchemaDetail, SchemaList, Method } from './resource/types';
export {
CacheProvider,
ExternalCacheProvider,
Expand Down
25 changes: 18 additions & 7 deletions packages/rest-hooks/src/resource/Resource.ts
@@ -1,3 +1,6 @@
/* istanbul ignore file */

import type { Method } from './types';
import SimpleResource from './SimpleResource';

class NetworkError extends Error {
Expand All @@ -16,17 +19,25 @@ class NetworkError extends Error {
* Typically 1:1 with a url endpoint.
*/
export default abstract class Resource extends SimpleResource {
/** A function to mutate all request options for fetch */
static fetchOptionsPlugin?: (options: RequestInit) => RequestInit;

/** Perform network request and resolve with HTTP Response */
static fetchResponse(input: RequestInfo, init: RequestInit) {
const options: RequestInit = {
...init,
static fetchResponse(
method: Method,
url: string,
body?: Readonly<object | string>,
) {
let options: RequestInit = {
method: method.toUpperCase(),
headers: {
'Content-Type': 'application/json',
// "Content-Type": "application/x-www-form-urlencoded", -- maybe use this if typeof body is FormData ?
...init.headers,
},
};
return fetch(input, options)
if (this.fetchOptionsPlugin) options = this.fetchOptionsPlugin(options);
if (body) options.body = JSON.stringify(body);
return fetch(url, options)
.then(response => {
if (!response.ok) {
throw new NetworkError(response);
Expand All @@ -43,8 +54,8 @@ export default abstract class Resource extends SimpleResource {
}

/** Perform network request and resolve with json body */
static fetch(input: RequestInfo, init: RequestInit) {
return this.fetchResponse(input, init).then((response: Response) => {
static fetch(method: Method, url: string, body?: Readonly<object | string>) {
return this.fetchResponse(method, url, body).then((response: Response) => {
if (
!response.headers.get('content-type')?.includes('json') ||
response.status === 204
Expand Down

0 comments on commit fdd1f7c

Please sign in to comment.