@graphql-tools/github-loader 7.3.27 → 7.3.28-alpha-20230407131203-6663dff6

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/index.js CHANGED
@@ -7,6 +7,7 @@ const graphql_tag_pluck_1 = require("@graphql-tools/graphql-tag-pluck");
7
7
  const graphql_1 = require("graphql");
8
8
  const sync_fetch_1 = tslib_1.__importDefault(require("@ardatan/sync-fetch"));
9
9
  const fetch_1 = require("@whatwg-node/fetch");
10
+ const value_or_promise_1 = require("value-or-promise");
10
11
  // github:owner/name#ref:path/to/file
11
12
  function extractData(pointer) {
12
13
  const [repo, file] = pointer.split('#');
@@ -36,27 +37,34 @@ class GithubLoader {
36
37
  canLoadSync(pointer) {
37
38
  return typeof pointer === 'string' && pointer.toLowerCase().startsWith('github:');
38
39
  }
39
- async load(pointer, options) {
40
- if (!(await this.canLoad(pointer))) {
40
+ loadSyncOrAsync(pointer, options, fetchFn) {
41
+ if (!this.canLoadSync(pointer)) {
41
42
  return [];
42
43
  }
43
44
  const { owner, name, ref, path } = extractData(pointer);
44
- const fetch = options.customFetch || fetch_1.fetch;
45
- const request = await fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
46
- const response = await request.json();
47
- const status = request.status;
48
- return this.handleResponse({ pointer, path, options, response, status });
45
+ return new value_or_promise_1.ValueOrPromise(() => fetchFn('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options })))
46
+ .then(response => {
47
+ const contentType = response.headers.get('content-type');
48
+ if (contentType && contentType.includes('application/json')) {
49
+ return response.json();
50
+ }
51
+ else {
52
+ return response.text();
53
+ }
54
+ })
55
+ .then(response => {
56
+ const status = response.status;
57
+ return this.handleResponse({ pointer, path, options, response, status });
58
+ })
59
+ .resolve();
60
+ }
61
+ load(pointer, options) {
62
+ const fetchFn = options.customFetch || fetch_1.fetch;
63
+ return this.loadSyncOrAsync(pointer, options, fetchFn);
49
64
  }
50
65
  loadSync(pointer, options) {
51
- if (!this.canLoadSync(pointer)) {
52
- return [];
53
- }
54
- const { owner, name, ref, path } = extractData(pointer);
55
- const fetch = options.customFetch || sync_fetch_1.default;
56
- const request = fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
57
- const response = request.json();
58
- const status = request.status;
59
- return this.handleResponse({ pointer, path, options, response, status });
66
+ const fetchFn = options.customFetch || sync_fetch_1.default;
67
+ return this.loadSyncOrAsync(pointer, options, fetchFn);
60
68
  }
61
69
  handleResponse({ pointer, path, options, response, status, }) {
62
70
  let errorMessage = null;
@@ -66,12 +74,18 @@ class GithubLoader {
66
74
  else if (status === 401) {
67
75
  errorMessage = response.message;
68
76
  }
77
+ else if (response.message) {
78
+ errorMessage = response.message;
79
+ }
69
80
  else if (!response.data) {
70
81
  errorMessage = response;
71
82
  }
72
83
  if (errorMessage) {
73
84
  throw new Error('Unable to download schema from github: ' + errorMessage);
74
85
  }
86
+ if (!response.data.repository.object) {
87
+ throw new Error(`Unable to find file: ${path} on ${pointer.replace(`:${path}`, '')}`);
88
+ }
75
89
  const content = response.data.repository.object.text;
76
90
  if (/\.(gql|graphql)s?$/i.test(path)) {
77
91
  return [(0, utils_1.parseGraphQLSDL)(pointer, content, options)];
@@ -89,12 +103,16 @@ class GithubLoader {
89
103
  throw new Error(`Invalid file extension: ${path}`);
90
104
  }
91
105
  prepareRequest({ owner, ref, path, name, options, }) {
106
+ const headers = {
107
+ 'content-type': 'application/json; charset=utf-8',
108
+ 'user-agent': 'graphql-tools',
109
+ };
110
+ if (options.token) {
111
+ headers['authorization'] = `bearer ${options.token}`;
112
+ }
92
113
  return {
93
114
  method: 'POST',
94
- headers: {
95
- 'content-type': 'application/json; charset=utf-8',
96
- authorization: `bearer ${options.token}`,
97
- },
115
+ headers,
98
116
  body: JSON.stringify({
99
117
  query: `
100
118
  query GetGraphQLSchemaForGraphQLtools($owner: String!, $name: String!, $expression: String!) {
package/esm/index.js CHANGED
@@ -3,6 +3,7 @@ import { gqlPluckFromCodeStringSync } from '@graphql-tools/graphql-tag-pluck';
3
3
  import { parse } from 'graphql';
4
4
  import syncFetch from '@ardatan/sync-fetch';
5
5
  import { fetch as asyncFetch } from '@whatwg-node/fetch';
6
+ import { ValueOrPromise } from 'value-or-promise';
6
7
  // github:owner/name#ref:path/to/file
7
8
  function extractData(pointer) {
8
9
  const [repo, file] = pointer.split('#');
@@ -32,27 +33,34 @@ export class GithubLoader {
32
33
  canLoadSync(pointer) {
33
34
  return typeof pointer === 'string' && pointer.toLowerCase().startsWith('github:');
34
35
  }
35
- async load(pointer, options) {
36
- if (!(await this.canLoad(pointer))) {
36
+ loadSyncOrAsync(pointer, options, fetchFn) {
37
+ if (!this.canLoadSync(pointer)) {
37
38
  return [];
38
39
  }
39
40
  const { owner, name, ref, path } = extractData(pointer);
40
- const fetch = options.customFetch || asyncFetch;
41
- const request = await fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
42
- const response = await request.json();
43
- const status = request.status;
44
- return this.handleResponse({ pointer, path, options, response, status });
41
+ return new ValueOrPromise(() => fetchFn('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options })))
42
+ .then(response => {
43
+ const contentType = response.headers.get('content-type');
44
+ if (contentType && contentType.includes('application/json')) {
45
+ return response.json();
46
+ }
47
+ else {
48
+ return response.text();
49
+ }
50
+ })
51
+ .then(response => {
52
+ const status = response.status;
53
+ return this.handleResponse({ pointer, path, options, response, status });
54
+ })
55
+ .resolve();
56
+ }
57
+ load(pointer, options) {
58
+ const fetchFn = options.customFetch || asyncFetch;
59
+ return this.loadSyncOrAsync(pointer, options, fetchFn);
45
60
  }
46
61
  loadSync(pointer, options) {
47
- if (!this.canLoadSync(pointer)) {
48
- return [];
49
- }
50
- const { owner, name, ref, path } = extractData(pointer);
51
- const fetch = options.customFetch || syncFetch;
52
- const request = fetch('https://api.github.com/graphql', this.prepareRequest({ owner, ref, path, name, options }));
53
- const response = request.json();
54
- const status = request.status;
55
- return this.handleResponse({ pointer, path, options, response, status });
62
+ const fetchFn = options.customFetch || syncFetch;
63
+ return this.loadSyncOrAsync(pointer, options, fetchFn);
56
64
  }
57
65
  handleResponse({ pointer, path, options, response, status, }) {
58
66
  let errorMessage = null;
@@ -62,12 +70,18 @@ export class GithubLoader {
62
70
  else if (status === 401) {
63
71
  errorMessage = response.message;
64
72
  }
73
+ else if (response.message) {
74
+ errorMessage = response.message;
75
+ }
65
76
  else if (!response.data) {
66
77
  errorMessage = response;
67
78
  }
68
79
  if (errorMessage) {
69
80
  throw new Error('Unable to download schema from github: ' + errorMessage);
70
81
  }
82
+ if (!response.data.repository.object) {
83
+ throw new Error(`Unable to find file: ${path} on ${pointer.replace(`:${path}`, '')}`);
84
+ }
71
85
  const content = response.data.repository.object.text;
72
86
  if (/\.(gql|graphql)s?$/i.test(path)) {
73
87
  return [parseGraphQLSDL(pointer, content, options)];
@@ -85,12 +99,16 @@ export class GithubLoader {
85
99
  throw new Error(`Invalid file extension: ${path}`);
86
100
  }
87
101
  prepareRequest({ owner, ref, path, name, options, }) {
102
+ const headers = {
103
+ 'content-type': 'application/json; charset=utf-8',
104
+ 'user-agent': 'graphql-tools',
105
+ };
106
+ if (options.token) {
107
+ headers['authorization'] = `bearer ${options.token}`;
108
+ }
88
109
  return {
89
110
  method: 'POST',
90
- headers: {
91
- 'content-type': 'application/json; charset=utf-8',
92
- authorization: `bearer ${options.token}`,
93
- },
111
+ headers,
94
112
  body: JSON.stringify({
95
113
  query: `
96
114
  query GetGraphQLSchemaForGraphQLtools($owner: String!, $name: String!, $expression: String!) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/github-loader",
3
- "version": "7.3.27",
3
+ "version": "7.3.28-alpha-20230407131203-6663dff6",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -8,9 +8,11 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@ardatan/sync-fetch": "^0.0.1",
11
+ "@graphql-tools/executor-http": "^0.1.9",
11
12
  "@graphql-tools/utils": "^9.2.1",
12
13
  "@graphql-tools/graphql-tag-pluck": "^7.4.6",
13
14
  "@whatwg-node/fetch": "^0.8.0",
15
+ "value-or-promise": "^1.0.12",
14
16
  "tslib": "^2.4.0"
15
17
  },
16
18
  "repository": {
@@ -1,7 +1,6 @@
1
1
  import { Loader, BaseLoaderOptions, Source } from '@graphql-tools/utils';
2
2
  import { GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck';
3
- import syncFetch from '@ardatan/sync-fetch';
4
- import { fetch as asyncFetch } from '@whatwg-node/fetch';
3
+ import { AsyncFetchFn, FetchFn, SyncFetchFn } from '@graphql-tools/executor-http';
5
4
  /**
6
5
  * Additional options for loading from GitHub
7
6
  */
@@ -9,12 +8,12 @@ export interface GithubLoaderOptions extends BaseLoaderOptions {
9
8
  /**
10
9
  * A GitHub access token
11
10
  */
12
- token: string;
11
+ token?: string;
13
12
  /**
14
13
  * Additional options to pass to `graphql-tag-pluck`
15
14
  */
16
15
  pluckConfig?: GraphQLTagPluckOptions;
17
- customFetch?: typeof asyncFetch | typeof syncFetch;
16
+ customFetch?: FetchFn;
18
17
  }
19
18
  /**
20
19
  * This loader loads a file from GitHub.
@@ -29,6 +28,8 @@ export interface GithubLoaderOptions extends BaseLoaderOptions {
29
28
  export declare class GithubLoader implements Loader<GithubLoaderOptions> {
30
29
  canLoad(pointer: string): Promise<boolean>;
31
30
  canLoadSync(pointer: string): boolean;
31
+ loadSyncOrAsync(pointer: string, options: GithubLoaderOptions, asyncFetchFn: AsyncFetchFn): Promise<Source[]>;
32
+ loadSyncOrAsync(pointer: string, options: GithubLoaderOptions, syncFetchFn: SyncFetchFn): Source[];
32
33
  load(pointer: string, options: GithubLoaderOptions): Promise<Source[]>;
33
34
  loadSync(pointer: string, options: GithubLoaderOptions): Source[];
34
35
  handleResponse({ pointer, path, options, response, status, }: {
@@ -1,7 +1,6 @@
1
1
  import { Loader, BaseLoaderOptions, Source } from '@graphql-tools/utils';
2
2
  import { GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck';
3
- import syncFetch from '@ardatan/sync-fetch';
4
- import { fetch as asyncFetch } from '@whatwg-node/fetch';
3
+ import { AsyncFetchFn, FetchFn, SyncFetchFn } from '@graphql-tools/executor-http';
5
4
  /**
6
5
  * Additional options for loading from GitHub
7
6
  */
@@ -9,12 +8,12 @@ export interface GithubLoaderOptions extends BaseLoaderOptions {
9
8
  /**
10
9
  * A GitHub access token
11
10
  */
12
- token: string;
11
+ token?: string;
13
12
  /**
14
13
  * Additional options to pass to `graphql-tag-pluck`
15
14
  */
16
15
  pluckConfig?: GraphQLTagPluckOptions;
17
- customFetch?: typeof asyncFetch | typeof syncFetch;
16
+ customFetch?: FetchFn;
18
17
  }
19
18
  /**
20
19
  * This loader loads a file from GitHub.
@@ -29,6 +28,8 @@ export interface GithubLoaderOptions extends BaseLoaderOptions {
29
28
  export declare class GithubLoader implements Loader<GithubLoaderOptions> {
30
29
  canLoad(pointer: string): Promise<boolean>;
31
30
  canLoadSync(pointer: string): boolean;
31
+ loadSyncOrAsync(pointer: string, options: GithubLoaderOptions, asyncFetchFn: AsyncFetchFn): Promise<Source[]>;
32
+ loadSyncOrAsync(pointer: string, options: GithubLoaderOptions, syncFetchFn: SyncFetchFn): Source[];
32
33
  load(pointer: string, options: GithubLoaderOptions): Promise<Source[]>;
33
34
  loadSync(pointer: string, options: GithubLoaderOptions): Source[];
34
35
  handleResponse({ pointer, path, options, response, status, }: {