Skip to content

Commit 5a13419

Browse files
ardatanAlan-Cha
authored andcommitted
Replace deprecated request with cross-fetch
Fix urljoin issue Fix issues Extract setSearchParams and replace global['fetch'] with typeof crossFetch
1 parent 5204f38 commit 5a13419

File tree

13 files changed

+7238
-578
lines changed

13 files changed

+7238
-578
lines changed

packages/openapi-to-graphql-cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
"dependencies": {
4545
"commander": "^6.1.0",
4646
"cors": "^2.8.5",
47+
"cross-fetch": "3.1.4",
4748
"express": "^4.16.4",
4849
"express-graphql": "^0.11.0",
4950
"graphql": "^15.3.0",
5051
"js-yaml": "^3.14.0",
51-
"openapi-to-graphql": "^2.4.0",
52-
"request": "^2.88.0"
52+
"openapi-to-graphql": "^2.4.0"
5353
},
5454
"devDependencies": {
5555
"@types/node": "^14.11.2",

packages/openapi-to-graphql-cli/src/index.ts

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import express from 'express'
22
import { graphqlHTTP } from 'express-graphql'
33
import cors from 'cors'
44
import path from 'path'
5-
import request from 'request'
65
import fs from 'fs'
76
import yaml from 'js-yaml'
87
import { printSchema } from 'graphql'
@@ -210,39 +209,28 @@ function readFile(path): Oas3 {
210209
* reads a remote file content using http protocol
211210
* @param {string} url specifies a valid URL path including the port number
212211
*/
213-
function getRemoteFileSpec(uri): Promise<Oas3> {
214-
return new Promise<Oas3>((resolve, reject) => {
215-
request(
216-
{
217-
uri
218-
},
219-
(err, res, body) => {
220-
if (err) {
221-
reject(err)
222-
} else if (res.statusCode < 200 && res.statusCode <= 300) {
223-
reject(
224-
new Error(
225-
`Could not retrieve file. Received unsuccessful status code '${res.statusCode}.`
226-
)
227-
)
228-
} else {
229-
if (typeof body === 'string') {
230-
try {
231-
resolve(JSON.parse(body))
232-
} catch (e) {
233-
try {
234-
resolve(yaml.safeLoad(body))
235-
} catch (f) {
236-
console.error(`JSON parse error: ${e}\nYAML parse error: ${f}`)
237-
}
238-
}
239-
}
240-
241-
reject(new Error(`Cannot parse remote file`))
212+
async function getRemoteFileSpec(uri): Promise<Oas3> {
213+
const res = await fetch(uri)
214+
const body = await res.text()
215+
if (res.status < 200 && res.status <= 300) {
216+
throw new Error(
217+
`Could not retrieve file. Received unsuccessful status code '${res.status}.`
218+
)
219+
} else {
220+
if (typeof body === 'string') {
221+
try {
222+
return JSON.parse(body)
223+
} catch (e) {
224+
try {
225+
return yaml.safeLoad(body)
226+
} catch (f) {
227+
console.error(`JSON parse error: ${e}\nYAML parse error: ${f}`)
242228
}
243229
}
244-
)
245-
})
230+
}
231+
232+
throw new Error(`Cannot parse remote file`)
233+
}
246234
}
247235

248236
/**

packages/openapi-to-graphql/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"testRegex": "/test/.*\\.test\\.(ts|tsx|js)$"
8282
},
8383
"dependencies": {
84+
"cross-fetch": "^3.1.4",
8485
"debug": "^4.2.0",
8586
"deep-equal": "^2.0.5",
8687
"form-urlencoded": "^6.0.4",
@@ -90,9 +91,9 @@
9091
"jsonpath-plus": "^4.0.0",
9192
"oas-validator": "^5.0.2",
9293
"pluralize": "^8.0.0",
93-
"request": "^2.88.0",
9494
"swagger2openapi": "^7.0.2",
95-
"tslib": "^2.3.0"
95+
"tslib": "^2.3.0",
96+
"url-join": "4.0.1"
9697
},
9798
"peerDependencies": {
9899
"graphql": "^14.0.0 || ^15.0.0"
@@ -101,8 +102,8 @@
101102
"@types/graphql": "^14.0.3",
102103
"@types/jest": "^26.0.14",
103104
"@types/node": "^14.11.2",
104-
"@types/request": "^2.48.1",
105105
"@types/deep-equal": "^1.0.1",
106+
"@types/url-join": "4.0.0",
106107
"aedes": "^0.42.6",
107108
"aedes-persistence": "^8.1.1",
108109
"body-parser": "^1.18.3",

packages/openapi-to-graphql/src/auth_builder.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import * as Oas3Tools from './oas_3_tools'
2929
import debug from 'debug'
3030
import { handleWarning, sortObject, MitigationTypes } from './utils'
3131
import { createDataDef } from './preprocessor'
32+
import crossFetch from 'cross-fetch'
3233

3334
const translationLog = debug('translation')
3435

@@ -41,7 +42,8 @@ const translationLog = debug('translation')
4142
export function createAndLoadViewer<TSource, TContext, TArgs>(
4243
queryFields: object,
4344
operationType: GraphQLOperationType,
44-
data: PreprocessingData<TSource, TContext, TArgs>
45+
data: PreprocessingData<TSource, TContext, TArgs>,
46+
fetch: typeof crossFetch
4547
): { [key: string]: GraphQLFieldConfig<TSource, TContext, TArgs> } {
4648
const results = {}
4749
/**
@@ -146,7 +148,8 @@ export function createAndLoadViewer<TSource, TContext, TArgs>(
146148
results[anyAuthObjectName] = getViewerAnyAuthOT(
147149
anyAuthObjectName,
148150
anyAuthFields,
149-
data
151+
data,
152+
fetch
150153
)
151154

152155
return results
@@ -244,7 +247,8 @@ function getViewerOT<TSource, TContext, TArgs>(
244247
function getViewerAnyAuthOT<TSource, TContext, TArgs>(
245248
name: string,
246249
queryFields: GraphQLFieldConfigMap<any, any>,
247-
data: PreprocessingData<TSource, TContext, TArgs>
250+
data: PreprocessingData<TSource, TContext, TArgs>,
251+
fetch: typeof crossFetch
248252
): GraphQLFieldConfig<TSource, TContext, TArgs> {
249253
// Resolve function:
250254
const resolve: GraphQLFieldResolver<TSource, TContext, TArgs> = (
@@ -275,7 +279,8 @@ function getViewerAnyAuthOT<TSource, TContext, TArgs>(
275279
const type = getGraphQLType({
276280
def,
277281
data,
278-
isInputObjectType: true
282+
isInputObjectType: true,
283+
fetch
279284
})
280285

281286
const saneProtocolName = Oas3Tools.sanitize(

packages/openapi-to-graphql/src/index.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import {
5252
GraphQLObjectType,
5353
GraphQLOutputType,
5454
GraphQLFieldConfig,
55-
GraphQLFieldConfigMap
5655
} from 'graphql'
5756

5857
// Imports:
@@ -66,14 +65,14 @@ import * as GraphQLTools from './graphql_tools'
6665
import { preprocessOas } from './preprocessor'
6766
import * as Oas3Tools from './oas_3_tools'
6867
import { createAndLoadViewer } from './auth_builder'
69-
import debug from 'debug'
7068
import { GraphQLSchemaConfig } from 'graphql/type/schema'
7169
import { sortObject, handleWarning, MitigationTypes } from './utils'
72-
73-
export { Oas2, Oas3, Options };
74-
70+
import crossFetch from 'cross-fetch'
71+
import debug from 'debug'
7572
const translationLog = debug('translation')
7673

74+
export { Oas2, Oas3, Options }
75+
7776
type Result<TSource, TContext, TArgs> = {
7877
schema: GraphQLSchema
7978
report: Report
@@ -124,7 +123,9 @@ const DEFAULT_OPTIONS: InternalOptions<any, any, any> = {
124123

125124
// Logging options
126125
provideErrorExtensions: true,
127-
equivalentToMessages: true
126+
equivalentToMessages: true,
127+
128+
fetch: crossFetch
128129
}
129130

130131
/**
@@ -208,7 +209,9 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
208209

209210
// Logging options
210211
provideErrorExtensions,
211-
equivalentToMessages
212+
equivalentToMessages,
213+
214+
fetch
212215
}: InternalOptions<TSource, TContext, TArgs>
213216
): Result<TSource, TContext, TArgs> {
214217
const options = {
@@ -247,7 +250,9 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
247250

248251
// Logging options
249252
provideErrorExtensions,
250-
equivalentToMessages
253+
equivalentToMessages,
254+
255+
fetch
251256
}
252257
translationLog(`Options: ${JSON.stringify(options)}`)
253258

@@ -370,7 +375,12 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
370375
if (Object.keys(authQueryFields).length > 0) {
371376
Object.assign(
372377
queryFields,
373-
createAndLoadViewer(authQueryFields, GraphQLOperationType.Query, data)
378+
createAndLoadViewer(
379+
authQueryFields,
380+
GraphQLOperationType.Query,
381+
data,
382+
fetch
383+
)
374384
)
375385
}
376386

@@ -380,7 +390,8 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
380390
createAndLoadViewer(
381391
authMutationFields,
382392
GraphQLOperationType.Mutation,
383-
data
393+
data,
394+
fetch
384395
)
385396
)
386397
}
@@ -391,7 +402,8 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
391402
createAndLoadViewer(
392403
authSubscriptionFields,
393404
GraphQLOperationType.Subscription,
394-
data
405+
data,
406+
fetch
395407
)
396408
)
397409
}
@@ -465,15 +477,17 @@ function addQueryFields<TSource, TContext, TArgs>({
465477
singularNames,
466478
baseUrl,
467479
requestOptions,
468-
connectOptions
480+
connectOptions,
481+
fetch
469482
} = options
470483

471484
const field = getFieldForOperation(
472485
operation,
473486
baseUrl,
474487
data,
475488
requestOptions,
476-
connectOptions
489+
connectOptions,
490+
fetch
477491
)
478492

479493
const saneOperationId = Oas3Tools.sanitize(
@@ -636,14 +650,21 @@ function addMutationFields<TSource, TContext, TArgs>({
636650
options: InternalOptions<TSource, TContext, TArgs>
637651
data: PreprocessingData<TSource, TContext, TArgs>
638652
}) {
639-
const { singularNames, baseUrl, requestOptions, connectOptions } = options
653+
const {
654+
singularNames,
655+
baseUrl,
656+
requestOptions,
657+
connectOptions,
658+
fetch
659+
} = options
640660

641661
const field = getFieldForOperation(
642662
operation,
643663
baseUrl,
644664
data,
645665
requestOptions,
646-
connectOptions
666+
connectOptions,
667+
fetch
647668
)
648669

649670
const saneOperationId = Oas3Tools.sanitize(
@@ -781,14 +802,15 @@ function addSubscriptionFields<TSource, TContext, TArgs>({
781802
options: InternalOptions<TSource, TContext, TArgs>
782803
data: PreprocessingData<TSource, TContext, TArgs>
783804
}) {
784-
const { baseUrl, requestOptions, connectOptions } = options
805+
const { baseUrl, requestOptions, connectOptions, fetch } = options
785806

786807
const field = getFieldForOperation(
787808
operation,
788809
baseUrl,
789810
data,
790811
requestOptions,
791-
connectOptions
812+
connectOptions,
813+
fetch
792814
)
793815

794816
const saneOperationId = Oas3Tools.sanitize(
@@ -890,13 +912,15 @@ function getFieldForOperation<TSource, TContext, TArgs>(
890912
baseUrl: string,
891913
data: PreprocessingData<TSource, TContext, TArgs>,
892914
requestOptions: Partial<RequestOptions<TSource, TContext, TArgs>>,
893-
connectOptions: ConnectOptions
915+
connectOptions: ConnectOptions,
916+
fetch: typeof crossFetch
894917
): GraphQLFieldConfig<TSource, TContext | SubscriptionContext, TArgs> {
895918
// Create GraphQL Type for response:
896919
const type = getGraphQLType({
897920
def: operation.responseDefinition,
898921
data,
899-
operation
922+
operation,
923+
fetch
900924
}) as GraphQLOutputType
901925

902926
const payloadSchemaName = operation.payloadDefinition
@@ -913,7 +937,8 @@ function getFieldForOperation<TSource, TContext, TArgs>(
913937
requestPayloadDef: operation.payloadDefinition,
914938
parameters: operation.parameters,
915939
operation,
916-
data
940+
data,
941+
fetch
917942
})
918943

919944
// Get resolver and subscribe function for Subscription fields
@@ -925,7 +950,8 @@ function getFieldForOperation<TSource, TContext, TArgs>(
925950
const resolve = getPublishResolver({
926951
operation,
927952
responseName: responseSchemaName,
928-
data
953+
data,
954+
fetch
929955
})
930956

931957
const subscribe = getSubscribe({
@@ -951,7 +977,8 @@ function getFieldForOperation<TSource, TContext, TArgs>(
951977
payloadName: payloadSchemaName,
952978
data,
953979
baseUrl,
954-
requestOptions
980+
requestOptions,
981+
fetch
955982
})
956983

957984
return {

0 commit comments

Comments
 (0)