Skip to content

Commit

Permalink
Merge pull request #33 from NullDivision/chore/PackageUpgrades
Browse files Browse the repository at this point in the history
Chore/package upgrades
  • Loading branch information
NullDivision authored Jul 2, 2021
2 parents cfa5941 + 8019083 commit 7e4d465
Show file tree
Hide file tree
Showing 7 changed files with 2,469 additions and 2,994 deletions.
34 changes: 16 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"test": "test"
},
"scripts": {
"build:clear": "rimraf -rfv ./dist/*",
"build:clear": "rm -rf ./dist/*",
"build:copy": "mkdirp ./dist/templates && copyfiles ./src/templates ./dist",
"build": "npm run build:clear && npm run build:copy && tsc",
"lint": "eslint ./src/**/*.ts",
"pretest": "rimraf -rf ./tmp/*",
"pretest": "rm -rf ./tmp/*",
"test": "jest"
},
"repository": {
Expand All @@ -39,26 +39,24 @@
"homepage": "https://github.com/NullDivision/doctyped#readme",
"dependencies": {
"ejs": "^3.0.1",
"request": "^2.88.0",
"request-promise-native": "^1.0.5",
"yargs": "^15.1.0"
"got": "^11.8.2",
"yargs": "^17.0.1"
},
"devDependencies": {
"@types/ejs": "^3.0.0",
"@types/jest": "^24.0.15",
"@types/request-promise-native": "^1.0.16",
"@types/yargs": "^13.0.0",
"@typescript-eslint/eslint-plugin": "^2.14.0",
"@typescript-eslint/parser": "^2.14.0",
"@types/jest": "^26.0.23",
"@types/yargs": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^4.28.1",
"@typescript-eslint/parser": "^4.28.1",
"copyfiles": "^2.1.1",
"eslint": "^6.1.0",
"flow-bin": "^0.115.0",
"flow-parser": "^0.115.0",
"jest": "^24.8.0",
"mkdirp": "^0.5.1",
"rimraf": "^3.0.0",
"ts-jest": "^24.0.2",
"typescript": "^3.1.6"
"eslint": "^7.29.0",
"flow-bin": "^0.154.0",
"flow-parser": "^0.154.0",
"jest": "^27.0.6",
"mkdirp": "^1.0.4",
"nock": "^13.1.0",
"ts-jest": "^27.0.3",
"typescript": "^3.9.10"
},
"jest": {
"preset": "ts-jest",
Expand Down
40 changes: 20 additions & 20 deletions src/__tests__/reader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import http = require('http');
import nock = require('nock');
import { API_TYPE } from '../builder';
import { getDescriptorResolver } from '../reader';

jest.mock('http');
jest.mock('request-promise-native');

describe('reader', () => {
beforeAll(() => {
nock.disableNetConnect();
});

afterAll(() => {
nock.enableNetConnect();
});

it('resolves graphql requests', async () => {
const TEST_URL = 'http://localhost';
const TEST_URL = 'http://localhost/graphiql';
const TEST_DATA = { data: { __schema: {} } };

jest
.requireMock('request-promise-native')
.post.mockResolvedValue(TEST_DATA);
nock('http://localhost').post('/graphiql').reply(200, TEST_DATA);

const result = await getDescriptorResolver(jest.requireMock('http'))(
const result = await getDescriptorResolver(http)(
API_TYPE.GRAPHQL,
{ uri: TEST_URL }
);
Expand All @@ -26,17 +31,12 @@ describe('reader', () => {

const TEST_STATUS = "I'm a teapot";

jest
.requireMock('request-promise-native')
.post
.mockRejectedValue(new Error(TEST_STATUS));

try {
await getDescriptorResolver(jest.requireMock('http'))(API_TYPE.GRAPHQL, {
uri: ''
});
} catch (error) {
expect(error.message).toBe(TEST_STATUS);
}
nock('http://localhost').post('/graphiql').replyWithError(TEST_STATUS);

return expect(
getDescriptorResolver(http)(API_TYPE.GRAPHQL, {
uri: 'http://localhost/graphiql'
})
).rejects.toThrowError(TEST_STATUS)
});
});
2 changes: 1 addition & 1 deletion src/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface SchemaValue {
}
export type Schema = ReadonlyArray<SchemaValue>;

export function build(api: API_TYPE) {
export function build(api: API_TYPE): typeof buildGraphql | typeof buildSwagger {
if (api === API_TYPE.GRAPHQL) {
return buildGraphql;
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const mapSwaggerTypes = (name: string, value: DescriptorValue): SchemaValue => {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
return { ...acc, [propName]: getProperty(propName, prop) };
}, {} as SchemaValue['properties']);
}, {});

return { name, properties: resolvedProperties };
};
Expand Down
38 changes: 20 additions & 18 deletions src/reader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { promises as fs } from 'fs';
import * as http from 'http';
import * as https from 'https';
import { post as request } from 'request-promise-native';
import got from 'got';

import { API_TYPE } from './builder';
import logger from './logger';
Expand Down Expand Up @@ -98,23 +98,25 @@ const resolveSwaggerDescriptor = (
};

const resolveGraphqlDescriptor = () => async (
options: Parameters<typeof request>['0']
url: string,
headers?: { Authorization?: string }
): Promise<GraphQlResponse> => {
const opts = {
body: {
query:
'query IntrospectionQuery {\n __schema {\n queryType {\n name\n }\n mutationType {\n name\n }\n subscriptionType {\n name\n }\n types {\n ...FullType\n }\n directives {\n name\n locations\n args {\n ...InputValue\n }\n }\n }\n}\n\nfragment FullType on __Type {\n kind\n name\n fields {\n name\n args {\n ...InputValue\n }\n type {\n ...TypeRef\n }\n }\n inputFields {\n ...InputValue\n }\n interfaces {\n ...TypeRef\n }\n enumValues {\n name\n }\n possibleTypes {\n ...TypeRef\n }\n}\n\nfragment InputValue on __InputValue {\n name\n type {\n ...TypeRef\n }\n defaultValue\n}\n\nfragment TypeRef on __Type {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n }\n }\n }\n }\n }\n }\n }\n}',
variables: null,
operationName: 'IntrospectionQuery'
},
json: true,
rejectUnauthorized: false
};

try {
const {
data: { __schema }
} = await request({ ...opts, ...options });
const response = await got.post<{ data: { __schema: GraphQlResponse } }>(
url,
{
json: {
query:
'query IntrospectionQuery {\n __schema {\n queryType {\n name\n }\n mutationType {\n name\n }\n subscriptionType {\n name\n }\n types {\n ...FullType\n }\n directives {\n name\n locations\n args {\n ...InputValue\n }\n }\n }\n}\n\nfragment FullType on __Type {\n kind\n name\n fields {\n name\n args {\n ...InputValue\n }\n type {\n ...TypeRef\n }\n }\n inputFields {\n ...InputValue\n }\n interfaces {\n ...TypeRef\n }\n enumValues {\n name\n }\n possibleTypes {\n ...TypeRef\n }\n}\n\nfragment InputValue on __InputValue {\n name\n type {\n ...TypeRef\n }\n defaultValue\n}\n\nfragment TypeRef on __Type {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n }\n }\n }\n }\n }\n }\n }\n}',
variables: null,
operationName: 'IntrospectionQuery'
},
headers,
responseType: 'json',
retry: 0
}
);
const { body: { data: { __schema } } } = response;
return __schema;
} catch (err) {
logger(err.stack);
Expand All @@ -124,7 +126,7 @@ const resolveGraphqlDescriptor = () => async (
};

interface Options {
headers?: {};
headers?: { Authorization?: string };
uri: string;
}
export type ResponseType = Descriptor | GraphQlResponse | undefined;
Expand All @@ -136,7 +138,7 @@ export const getDescriptorResolver = (
case API_TYPE.SWAGGER:
return resolveSwaggerDescriptor(client)(options.uri);
case API_TYPE.GRAPHQL:
return resolveGraphqlDescriptor()(options);
return resolveGraphqlDescriptor()(options.uri, options.headers);
default:
throw new Error(
`Invalid api type '${api}' provided. Type must be one of: ${API_OPTS.join(
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
},
"files": ["src/index.ts"],
"exclude": ["tmp"]
}
}
Loading

0 comments on commit 7e4d465

Please sign in to comment.