@graphql-tools/github-loader 7.3.2-alpha-b93d3b57.0 → 7.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{cjs → esm}/index.js +12 -17
- package/package.json +1 -1
- package/typings/index.d.ts +50 -0
package/{cjs → esm}/index.js
RENAMED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const graphql_tag_pluck_1 = require("@graphql-tools/graphql-tag-pluck");
|
|
7
|
-
const graphql_1 = require("graphql");
|
|
8
|
-
const sync_fetch_1 = tslib_1.__importDefault(require("sync-fetch"));
|
|
9
|
-
const fetch_1 = require("@whatwg-node/fetch");
|
|
1
|
+
import { parseGraphQLSDL, parseGraphQLJSON } from '@graphql-tools/utils';
|
|
2
|
+
import { gqlPluckFromCodeStringSync } from '@graphql-tools/graphql-tag-pluck';
|
|
3
|
+
import { parse } from 'graphql';
|
|
4
|
+
import syncFetch from 'sync-fetch';
|
|
5
|
+
import { fetch as asyncFetch } from '@whatwg-node/fetch';
|
|
10
6
|
// github:owner/name#ref:path/to/file
|
|
11
7
|
function extractData(pointer) {
|
|
12
8
|
const [repo, file] = pointer.split('#');
|
|
@@ -29,7 +25,7 @@ function extractData(pointer) {
|
|
|
29
25
|
* })
|
|
30
26
|
* ```
|
|
31
27
|
*/
|
|
32
|
-
class GithubLoader {
|
|
28
|
+
export class GithubLoader {
|
|
33
29
|
async canLoad(pointer) {
|
|
34
30
|
return this.canLoadSync(pointer);
|
|
35
31
|
}
|
|
@@ -41,7 +37,7 @@ class GithubLoader {
|
|
|
41
37
|
return [];
|
|
42
38
|
}
|
|
43
39
|
const { owner, name, ref, path } = extractData(pointer);
|
|
44
|
-
const fetch = options.customFetch ||
|
|
40
|
+
const fetch = options.customFetch || asyncFetch;
|
|
45
41
|
const request = await fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
|
|
46
42
|
const response = await request.json();
|
|
47
43
|
return this.handleResponse({ pointer, path, options, response });
|
|
@@ -51,7 +47,7 @@ class GithubLoader {
|
|
|
51
47
|
return [];
|
|
52
48
|
}
|
|
53
49
|
const { owner, name, ref, path } = extractData(pointer);
|
|
54
|
-
const fetch = options.customFetch ||
|
|
50
|
+
const fetch = options.customFetch || syncFetch;
|
|
55
51
|
const request = fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
|
|
56
52
|
const response = request.json();
|
|
57
53
|
return this.handleResponse({ pointer, path, options, response });
|
|
@@ -69,16 +65,16 @@ class GithubLoader {
|
|
|
69
65
|
}
|
|
70
66
|
const content = response.data.repository.object.text;
|
|
71
67
|
if (/\.(gql|graphql)s?$/i.test(path)) {
|
|
72
|
-
return [
|
|
68
|
+
return [parseGraphQLSDL(pointer, content, options)];
|
|
73
69
|
}
|
|
74
70
|
if (/\.json$/i.test(path)) {
|
|
75
|
-
return [
|
|
71
|
+
return [parseGraphQLJSON(pointer, content, options)];
|
|
76
72
|
}
|
|
77
73
|
if (path.endsWith('.tsx') || path.endsWith('.ts') || path.endsWith('.js') || path.endsWith('.jsx')) {
|
|
78
|
-
const sources =
|
|
74
|
+
const sources = gqlPluckFromCodeStringSync(pointer, content, options.pluckConfig);
|
|
79
75
|
return sources.map(source => ({
|
|
80
76
|
location: pointer,
|
|
81
|
-
document:
|
|
77
|
+
document: parse(source, options),
|
|
82
78
|
}));
|
|
83
79
|
}
|
|
84
80
|
throw new Error(`Invalid file extension: ${path}`);
|
|
@@ -112,4 +108,3 @@ class GithubLoader {
|
|
|
112
108
|
};
|
|
113
109
|
}
|
|
114
110
|
}
|
|
115
|
-
exports.GithubLoader = GithubLoader;
|
package/package.json
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Loader, BaseLoaderOptions, Source } from '@graphql-tools/utils';
|
|
2
|
+
import { GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck';
|
|
3
|
+
import syncFetch from 'sync-fetch';
|
|
4
|
+
import { fetch as asyncFetch } from '@whatwg-node/fetch';
|
|
5
|
+
/**
|
|
6
|
+
* Additional options for loading from GitHub
|
|
7
|
+
*/
|
|
8
|
+
export interface GithubLoaderOptions extends BaseLoaderOptions {
|
|
9
|
+
/**
|
|
10
|
+
* A GitHub access token
|
|
11
|
+
*/
|
|
12
|
+
token: string;
|
|
13
|
+
/**
|
|
14
|
+
* Additional options to pass to `graphql-tag-pluck`
|
|
15
|
+
*/
|
|
16
|
+
pluckConfig?: GraphQLTagPluckOptions;
|
|
17
|
+
customFetch?: typeof asyncFetch | typeof syncFetch;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* This loader loads a file from GitHub.
|
|
21
|
+
*
|
|
22
|
+
* ```js
|
|
23
|
+
* const typeDefs = await loadTypedefs('github:githubUser/githubRepo#branchName:path/to/file.ts', {
|
|
24
|
+
* loaders: [new GithubLoader()],
|
|
25
|
+
* token: YOUR_GITHUB_TOKEN,
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare class GithubLoader implements Loader<GithubLoaderOptions> {
|
|
30
|
+
canLoad(pointer: string): Promise<boolean>;
|
|
31
|
+
canLoadSync(pointer: string): boolean;
|
|
32
|
+
load(pointer: string, options: GithubLoaderOptions): Promise<Source[]>;
|
|
33
|
+
loadSync(pointer: string, options: GithubLoaderOptions): Source[];
|
|
34
|
+
handleResponse({ pointer, path, options, response }: {
|
|
35
|
+
pointer: string;
|
|
36
|
+
path: string;
|
|
37
|
+
options: any;
|
|
38
|
+
response: any;
|
|
39
|
+
}): Source[] | {
|
|
40
|
+
location: string | undefined;
|
|
41
|
+
document: import("graphql").DocumentNode;
|
|
42
|
+
}[];
|
|
43
|
+
prepareRequest({ owner, ref, path, name, options, }: {
|
|
44
|
+
owner: string;
|
|
45
|
+
ref: string;
|
|
46
|
+
path: string;
|
|
47
|
+
name: string;
|
|
48
|
+
options: GithubLoaderOptions;
|
|
49
|
+
}): RequestInit;
|
|
50
|
+
}
|