@graphql-tools/github-loader
Advanced tools
Comparing version 7.3.27 to 7.3.28-alpha-20230407131203-6663dff6
@@ -10,2 +10,3 @@ "use strict"; | ||
const fetch_1 = require("@whatwg-node/fetch"); | ||
const value_or_promise_1 = require("value-or-promise"); | ||
// github:owner/name#ref:path/to/file | ||
@@ -40,23 +41,30 @@ function extractData(pointer) { | ||
} | ||
async load(pointer, options) { | ||
if (!(await this.canLoad(pointer))) { | ||
loadSyncOrAsync(pointer, options, fetchFn) { | ||
if (!this.canLoadSync(pointer)) { | ||
return []; | ||
} | ||
const { owner, name, ref, path } = extractData(pointer); | ||
const fetch = options.customFetch || fetch_1.fetch; | ||
const request = await fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options })); | ||
const response = await request.json(); | ||
const status = request.status; | ||
return this.handleResponse({ pointer, path, options, response, status }); | ||
return new value_or_promise_1.ValueOrPromise(() => fetchFn('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }))) | ||
.then(response => { | ||
const contentType = response.headers.get('content-type'); | ||
if (contentType && contentType.includes('application/json')) { | ||
return response.json(); | ||
} | ||
else { | ||
return response.text(); | ||
} | ||
}) | ||
.then(response => { | ||
const status = response.status; | ||
return this.handleResponse({ pointer, path, options, response, status }); | ||
}) | ||
.resolve(); | ||
} | ||
load(pointer, options) { | ||
const fetchFn = options.customFetch || fetch_1.fetch; | ||
return this.loadSyncOrAsync(pointer, options, fetchFn); | ||
} | ||
loadSync(pointer, options) { | ||
if (!this.canLoadSync(pointer)) { | ||
return []; | ||
} | ||
const { owner, name, ref, path } = extractData(pointer); | ||
const fetch = options.customFetch || sync_fetch_1.default; | ||
const request = fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options })); | ||
const response = request.json(); | ||
const status = request.status; | ||
return this.handleResponse({ pointer, path, options, response, status }); | ||
const fetchFn = options.customFetch || sync_fetch_1.default; | ||
return this.loadSyncOrAsync(pointer, options, fetchFn); | ||
} | ||
@@ -71,2 +79,5 @@ handleResponse({ pointer, path, options, response, status, }) { | ||
} | ||
else if (response.message) { | ||
errorMessage = response.message; | ||
} | ||
else if (!response.data) { | ||
@@ -78,2 +89,5 @@ errorMessage = response; | ||
} | ||
if (!response.data.repository.object) { | ||
throw new Error(`Unable to find file: ${path} on ${pointer.replace(`:${path}`, '')}`); | ||
} | ||
const content = response.data.repository.object.text; | ||
@@ -96,8 +110,12 @@ if (/\.(gql|graphql)s?$/i.test(path)) { | ||
prepareRequest({ owner, ref, path, name, options, }) { | ||
const headers = { | ||
'content-type': 'application/json; charset=utf-8', | ||
'user-agent': 'graphql-tools', | ||
}; | ||
if (options.token) { | ||
headers['authorization'] = `bearer ${options.token}`; | ||
} | ||
return { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json; charset=utf-8', | ||
authorization: `bearer ${options.token}`, | ||
}, | ||
headers, | ||
body: JSON.stringify({ | ||
@@ -104,0 +122,0 @@ query: ` |
@@ -6,2 +6,3 @@ import { parseGraphQLSDL, parseGraphQLJSON } from '@graphql-tools/utils'; | ||
import { fetch as asyncFetch } from '@whatwg-node/fetch'; | ||
import { ValueOrPromise } from 'value-or-promise'; | ||
// github:owner/name#ref:path/to/file | ||
@@ -36,23 +37,30 @@ function extractData(pointer) { | ||
} | ||
async load(pointer, options) { | ||
if (!(await this.canLoad(pointer))) { | ||
loadSyncOrAsync(pointer, options, fetchFn) { | ||
if (!this.canLoadSync(pointer)) { | ||
return []; | ||
} | ||
const { owner, name, ref, path } = extractData(pointer); | ||
const fetch = options.customFetch || asyncFetch; | ||
const request = await fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options })); | ||
const response = await request.json(); | ||
const status = request.status; | ||
return this.handleResponse({ pointer, path, options, response, status }); | ||
return new ValueOrPromise(() => fetchFn('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }))) | ||
.then(response => { | ||
const contentType = response.headers.get('content-type'); | ||
if (contentType && contentType.includes('application/json')) { | ||
return response.json(); | ||
} | ||
else { | ||
return response.text(); | ||
} | ||
}) | ||
.then(response => { | ||
const status = response.status; | ||
return this.handleResponse({ pointer, path, options, response, status }); | ||
}) | ||
.resolve(); | ||
} | ||
load(pointer, options) { | ||
const fetchFn = options.customFetch || asyncFetch; | ||
return this.loadSyncOrAsync(pointer, options, fetchFn); | ||
} | ||
loadSync(pointer, options) { | ||
if (!this.canLoadSync(pointer)) { | ||
return []; | ||
} | ||
const { owner, name, ref, path } = extractData(pointer); | ||
const fetch = options.customFetch || syncFetch; | ||
const request = fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options })); | ||
const response = request.json(); | ||
const status = request.status; | ||
return this.handleResponse({ pointer, path, options, response, status }); | ||
const fetchFn = options.customFetch || syncFetch; | ||
return this.loadSyncOrAsync(pointer, options, fetchFn); | ||
} | ||
@@ -67,2 +75,5 @@ handleResponse({ pointer, path, options, response, status, }) { | ||
} | ||
else if (response.message) { | ||
errorMessage = response.message; | ||
} | ||
else if (!response.data) { | ||
@@ -74,2 +85,5 @@ errorMessage = response; | ||
} | ||
if (!response.data.repository.object) { | ||
throw new Error(`Unable to find file: ${path} on ${pointer.replace(`:${path}`, '')}`); | ||
} | ||
const content = response.data.repository.object.text; | ||
@@ -92,8 +106,12 @@ if (/\.(gql|graphql)s?$/i.test(path)) { | ||
prepareRequest({ owner, ref, path, name, options, }) { | ||
const headers = { | ||
'content-type': 'application/json; charset=utf-8', | ||
'user-agent': 'graphql-tools', | ||
}; | ||
if (options.token) { | ||
headers['authorization'] = `bearer ${options.token}`; | ||
} | ||
return { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json; charset=utf-8', | ||
authorization: `bearer ${options.token}`, | ||
}, | ||
headers, | ||
body: JSON.stringify({ | ||
@@ -100,0 +118,0 @@ query: ` |
{ | ||
"name": "@graphql-tools/github-loader", | ||
"version": "7.3.27", | ||
"version": "7.3.28-alpha-20230407131203-6663dff6", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -11,5 +11,7 @@ "sideEffects": false, | ||
"@ardatan/sync-fetch": "^0.0.1", | ||
"@graphql-tools/executor-http": "^0.1.9", | ||
"@graphql-tools/utils": "^9.2.1", | ||
"@graphql-tools/graphql-tag-pluck": "^7.4.6", | ||
"@whatwg-node/fetch": "^0.8.0", | ||
"value-or-promise": "^1.0.12", | ||
"tslib": "^2.4.0" | ||
@@ -16,0 +18,0 @@ }, |
import { Loader, BaseLoaderOptions, Source } from '@graphql-tools/utils'; | ||
import { GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck'; | ||
import syncFetch from '@ardatan/sync-fetch'; | ||
import { fetch as asyncFetch } from '@whatwg-node/fetch'; | ||
import { AsyncFetchFn, FetchFn, SyncFetchFn } from '@graphql-tools/executor-http'; | ||
/** | ||
@@ -12,3 +11,3 @@ * Additional options for loading from GitHub | ||
*/ | ||
token: string; | ||
token?: string; | ||
/** | ||
@@ -18,3 +17,3 @@ * Additional options to pass to `graphql-tag-pluck` | ||
pluckConfig?: GraphQLTagPluckOptions; | ||
customFetch?: typeof asyncFetch | typeof syncFetch; | ||
customFetch?: FetchFn; | ||
} | ||
@@ -34,2 +33,4 @@ /** | ||
canLoadSync(pointer: string): boolean; | ||
loadSyncOrAsync(pointer: string, options: GithubLoaderOptions, asyncFetchFn: AsyncFetchFn): Promise<Source[]>; | ||
loadSyncOrAsync(pointer: string, options: GithubLoaderOptions, syncFetchFn: SyncFetchFn): Source[]; | ||
load(pointer: string, options: GithubLoaderOptions): Promise<Source[]>; | ||
@@ -36,0 +37,0 @@ loadSync(pointer: string, options: GithubLoaderOptions): Source[]; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
15230
323
0
8
2
+ Addedvalue-or-promise@^1.0.12
+ Added@graphql-tools/executor-http@0.1.10(transitive)
+ Added@repeaterjs/repeater@3.0.6(transitive)
+ Addeddset@3.1.4(transitive)
+ Addedelectron-to-chromium@1.5.47(transitive)
+ Addedextract-files@11.0.0(transitive)
+ Addedmeros@1.3.0(transitive)
+ Addedvalue-or-promise@1.0.12(transitive)
- Removedelectron-to-chromium@1.5.48(transitive)