Skip to content

Commit

Permalink
feat: replace xfetch with got
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jul 6, 2019
1 parent db0187d commit 65dde74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"core-js": "^3.1.4",
"deep-map-keys": "^2.0.1",
"es6-error": "^4.1.1",
"got": "^9.6.0",
"lodash": "^4.17.11",
"qs": "^6.7.0",
"roarr": "^2.13.2",
"xfetch": "^3.14.3"
"roarr": "^2.13.2"
},
"description": "TMDb SDK.",
"devDependencies": {
Expand All @@ -47,9 +47,9 @@
"eslint-config-canonical": "^17.1.1",
"flow-bin": "^0.102.0",
"husky": "^3.0.0",
"nock": "^10.0.6",
"nock": "^11.0.0-beta.20",
"nyc": "^14.1.1",
"semantic-release": "^15.13.18",
"semantic-release": "^15.13.19",
"sinon": "^7.3.2",
"travis-deploy-once": "^5.0.11"
},
Expand Down
38 changes: 15 additions & 23 deletions src/Tmdb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow

import xfetch from 'xfetch';
import qs from 'qs';
import got from 'got';
import deepMapKeys from 'deep-map-keys';
import {
delay
Expand Down Expand Up @@ -48,29 +47,26 @@ class Tmdb {
async get (resource: string, parameters: QueryType = {}): Object {
// eslint-disable-next-line no-constant-condition
while (true) {
const requestQuery = qs.stringify({
// eslint-disable-next-line id-match
api_key: this.apiKey,
...parameters
});

const response = await xfetch('https://api.themoviedb.org/3/' + resource + '?' + requestQuery, {
isResponseValid: () => {
return true;
const response = await got('https://api.themoviedb.org/3/' + resource, {
json: true,
query: {
// eslint-disable-next-line id-match
api_key: this.apiKey,
...parameters
},
responseType: 'full'
throwHttpErrors: false
});

if (!response.headers.has('x-ratelimit-remaining')) {
if (!response.headers['x-ratelimit-remaining']) {
throw new UnexpectedResponseError();
}

if (!String(response.status).startsWith('2')) {
const rateLimitRemaining = Number(response.headers.get('x-ratelimit-remaining'));
if (!String(response.statusCode).startsWith('2')) {
const rateLimitRemaining = Number(response.headers['x-ratelimit-remaining']);

if (!rateLimitRemaining) {
const currentTime = Math.round(new Date().getTime() / 1000);
const rateLimitReset = Number(response.headers.get('x-ratelimit-reset'));
const rateLimitReset = Number(response.headers['x-ratelimit-reset']);

// The minimum 30 seconds cooldown ensures that in case 'x-ratelimit-reset'
// time is wrong, we don't bombard the TMDb server with requests.
Expand All @@ -84,18 +80,14 @@ class Tmdb {
continue;
}

if (response.status === 404) {
if (response.statusCode === 404) {
throw new NotFoundError();
}

const errorBody = await response.json();

throw new RemoteError(errorBody.status_message, errorBody.status_code);
throw new RemoteError(response.body.status_message, response.body.status_code);
}

const body = await response.json();

return deepMapKeys(body, camelCase);
return deepMapKeys(response.body, camelCase);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/bin/generate-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @file A promitive script used to generate Flow type declarations using TMDb OAS output.
*/

import xfetch from 'xfetch';
import got from 'got';
import {
camelCase
} from 'lodash';
Expand Down Expand Up @@ -42,7 +42,7 @@ const definitionMap = {

const typeNames = Object.keys(typeMap);

const createFlowType = (typeDefinition: mixed) => {
const createFlowType = (typeDefinition) => {
if (Array.isArray(typeDefinition)) {
return typeDefinition.map(createFlowType).join(' | ');
} else if (typeof typeDefinition === 'string') {
Expand Down Expand Up @@ -121,8 +121,8 @@ const getPropertyFlowType = (property: Object) => {
};

const run = async () => {
const oas = await xfetch('https://api.stoplight.io/v1/versions/9WaNJfGpnnQ76opqe/export/oas.json', {
responseType: 'json'
const oas = await got('https://api.stoplight.io/v1/versions/9WaNJfGpnnQ76opqe/export/oas.json', {
json: true
});

for (const typeName of typeNames) {
Expand All @@ -132,7 +132,7 @@ const run = async () => {
throw new Error('Unexpected state.');
}

const typeDefinition = resourceResolver(oas);
const typeDefinition = resourceResolver(oas.body);

if (!typeDefinition) {
throw new Error('Unexpected state.');
Expand Down

0 comments on commit 65dde74

Please sign in to comment.