Skip to content

Commit

Permalink
Merge pull request #13 from 4Catalyzer/misc
Browse files Browse the repository at this point in the history
Misc improvements
  • Loading branch information
jquense authored Apr 20, 2018
2 parents d0f6c82 + 504373c commit acfc8da
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 47 deletions.
31 changes: 13 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,41 @@
"node": ">= 8.3.0"
},
"scripts": {
"build": "babel src -d lib --ignore __tests__ && BABEL_ENV=esm babel src --out-dir es --ignore __tests__ && npm run build:flow",
"build:flow": "for file in $(find ./src -name '*.js' -not -path '*/__tests__*'); do cp \"$file\" `echo \"$file\" | sed 's/\\/src\\//\\/lib\\//g'`.flow; cp \"$file\" `echo \"$file\" | sed 's/\\/src\\//\\/es\\//g'`.flow; done",
"build":
"babel src -d lib --ignore __tests__ && BABEL_ENV=esm babel src --out-dir es --ignore __tests__ && npm run build:flow",
"build:flow":
"for file in $(find ./src -name '*.js' -not -path '*/__tests__*'); do cp \"$file\" `echo \"$file\" | sed 's/\\/src\\//\\/lib\\//g'`.flow; cp \"$file\" `echo \"$file\" | sed 's/\\/src\\//\\/es\\//g'`.flow; done",
"check": "flow check",
"tdd": "jest --watch",
"test": "npm run lint && jest --coverage",
"lint": "npm run check && eslint . && prettier --list-different --ignore-path .eslintignore '**/*.{json,css}'",
"format": "eslint . --fix && prettier --write --ignore-path .eslintignore '**/*.{json,css,md}'",
"lint":
"npm run check && eslint . && prettier --list-different --ignore-path .eslintignore '**/*.{json,css}'",
"format":
"eslint . --fix && prettier --write --ignore-path .eslintignore '**/*.{json,css,md}'",
"precommit": "lint-staged",
"prepublishOnly": "npm run build",
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once"
},
"files": [
"lib",
"es"
],
"files": ["lib", "es"],
"jest": {
"collectCoverageFrom": [
"src/**"
]
"testEnvironment": "node",
"collectCoverageFrom": ["src/**"]
},
"prettier": {
"printWidth": 79,
"singleQuote": true,
"trailingComma": "all"
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
],
"*.js": ["eslint --fix", "git add"],
"*.{json,css,md}": [
"prettier --write --ignore-path .eslintignore",
"git add"
]
},
"release": {
"extends": [
"@4c/semantic-release-config"
]
"extends": ["@4c/semantic-release-config"]
},
"devDependencies": {
"@4c/semantic-release-config": "^1.0.1",
Expand Down
10 changes: 7 additions & 3 deletions src/api/HttpApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import pick from 'lodash/pick';
import invariant from 'invariant';
import querystring from 'querystring';

import HttpError from './HttpError';
import urlJoin from '../utils/urlJoin';
import HttpError from './HttpError';
import type { HttpMethod } from './fetch';

const PAGINATION_ARG_KEYS = Object.keys(forwardConnectionArgs);

Expand Down Expand Up @@ -150,8 +151,11 @@ export default class HttpApi {
};
}

// eslint-disable-next-line no-unused-vars
async request<T>(method: string, reqUrl: string, data?: Data): Promise<?T> {
async request<T>(
method: HttpMethod,
reqUrl: string,
data?: Data, // eslint-disable-line no-unused-vars
): Promise<?T> {
throw new Error('Not Implemented');
}

Expand Down
23 changes: 19 additions & 4 deletions src/api/HttpError.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* @flow */

import util from 'util';

export type JsonApiError = {
detail?: string,
code: string,
Expand All @@ -13,7 +15,7 @@ export default class HttpError extends Error {
response: Response;
status: number;
errors: Array<JsonApiError> = [];
body: ?string = null;
body: string = '';
extensions: ?{
upstream: {
status: number,
Expand All @@ -26,14 +28,13 @@ export default class HttpError extends Error {

this.response = response;
this.status = response.status;
this.message = `HttpError(${this.status}): A network request failed`;
}

async init() {
try {
this.body = await this.response.text();
this.errors = JSON.parse(this.body).errors;

this.message = this.errors && this.errors[0] && this.errors[0].detail;
this.errors = JSON.parse(this.body).errors || [];

this.extensions = {
upstream: {
Expand All @@ -44,5 +45,19 @@ export default class HttpError extends Error {
} catch (e) {
this.errors = [];
}

if (this.errors && this.errors.length) {
this.message = `HttpError(${
this.status
}): The network resource returned the following errors:\n\n${util.inspect(
this.errors,
)}`;
} else {
this.message = `HttpError(${
this.status
}): The network resource returned the following message:\n\n${
this.body
}`;
}
}
}
13 changes: 12 additions & 1 deletion src/api/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ type File = {
buffer: Buffer,
};

export type HttpMethod =
| 'GET'
| 'HEAD'
| 'POST'
| 'PUT'
| 'DELETE'
| 'CONNECT'
| 'OPTIONS'
| 'TRACE'
| 'PATCH';

export type RequestOptions = {
method: string,
method: HttpMethod,
url: string,
data?: ?mixed,
files?: File[],
Expand Down
57 changes: 57 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* @flow */

import HttpResource from './resources/HttpResource';
import PaginatedHttpResource from './resources/PaginatedHttpResource';
import Resource from './resources/Resource';

import HttpApi from './api/HttpApi';
import HttpError from './api/HttpError';
import fetch from './api/fetch';

import createNodeType from './types/NodeType';

import asType from './utils/asType';
import resolveThunk from './utils/resolveThunk';
import translateKeys from './utils/translateKeys';
import urlJoin from './utils/urlJoin';

export const utils = {
asType,
resolveThunk,
translateKeys,
urlJoin,
};

export {
HttpApi,
HttpError,
fetch,
HttpResource,
PaginatedHttpResource,
Resource,
createNodeType,
};

export type { HttpMethod, RequestOptions } from './api/fetch';
export type {
Args,
Data,
QueryString,
ValidationResult,
PaginationResult,
HttpApiOptions,
} from './api/HttpApi';

export type { JsonApiError } from './api/HttpError';
export type {
Endpoint,
HttpContext,
HttpResourceOptions,
} from './resources/HttpResource';

export {
NodeFieldResolver,
NodeFieldConfig,
NodeFieldConfigMap,
CreateNodeTypeArgs,
} from './types/NodeType';
2 changes: 1 addition & 1 deletion src/resources/PaginatedHttpResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import HttpResource from './HttpResource';
import type HttpApi, { Args } from '../api/HttpApi';

export default class PaginatedHttpResourceHttpResource<
export default class PaginatedHttpResource<
TApi: HttpApi = HttpApi,
> extends HttpResource<TApi> {
getConnectionBase(path: string, args: Args) {
Expand Down
4 changes: 2 additions & 2 deletions src/types/NodeType.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export type NodeFieldConfigMap<TContext> = GraphQLFieldConfigMap<
TContext,
>;

type FieldNameResolver<TContext> = (
export type FieldNameResolver<TContext> = (
fieldName: string,
obj: ObjStub,
args: mixed,
context: TContext,
info: GraphQLResolveInfo,
) => string;

type CreateNodeTypeArgs = {
export type CreateNodeTypeArgs = {
fieldNameResolver?: FieldNameResolver<*>,
getDefaultResourceConfig?: (name: string) => mixed,
};
Expand Down
3 changes: 1 addition & 2 deletions test/HttpApi.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/** @flow */

import mockedFetch from 'node-fetch';
import HttpApi from '../src/api/HttpApi';
import { HttpApi, HttpError } from '../src';

import { TestHttpApi } from './helpers';
import HttpError from '../src/api/HttpError';

describe('HttpApi', () => {
afterEach(() => {
Expand Down
10 changes: 9 additions & 1 deletion test/HttpError.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @flow */

import { Response } from 'node-fetch';
import HttpError from '../src/api/HttpError';
import { HttpError } from '../src';

describe('HttpError', () => {
it('should add status', () => {
Expand All @@ -16,6 +16,10 @@ describe('HttpError', () => {
}),
);
await err.init();

expect(err.message).toEqual(
"HttpError(409): The network resource returned the following errors:\n\n[ 'bar', 'baz' ]",
);
expect(err.errors).toEqual(['bar', 'baz']);
});

Expand All @@ -26,7 +30,11 @@ describe('HttpError', () => {
}),
);
await err.init();

expect(err.body).toEqual('{,}');
expect(err.message).toEqual(
'HttpError(409): The network resource returned the following message:\n\n{,}',
);
expect(err.errors).toEqual([]);
});
});
Loading

0 comments on commit acfc8da

Please sign in to comment.