@tomsd/github-repo-js 0.3.0 → 0.4.0

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.
@@ -0,0 +1,25 @@
1
+ import type { QueryGenqlSelection, Query, MutationGenqlSelection, Mutation } from './schema';
2
+ import { type FieldsSelection, type GraphqlOperation, type ClientOptions, GenqlError } from './runtime';
3
+ export type { FieldsSelection } from './runtime';
4
+ export { GenqlError };
5
+ export * from './schema';
6
+ export interface Client {
7
+ query<R extends QueryGenqlSelection>(request: R & {
8
+ __name?: string;
9
+ }): Promise<FieldsSelection<Query, R>>;
10
+ mutation<R extends MutationGenqlSelection>(request: R & {
11
+ __name?: string;
12
+ }): Promise<FieldsSelection<Mutation, R>>;
13
+ }
14
+ export declare const createClient: (options?: ClientOptions) => Client;
15
+ export declare const everything: {
16
+ __scalar: boolean;
17
+ };
18
+ export type QueryResult<fields extends QueryGenqlSelection> = FieldsSelection<Query, fields>;
19
+ export declare const generateQueryOp: (fields: QueryGenqlSelection & {
20
+ __name?: string;
21
+ }) => GraphqlOperation;
22
+ export type MutationResult<fields extends MutationGenqlSelection> = FieldsSelection<Mutation, fields>;
23
+ export declare const generateMutationOp: (fields: MutationGenqlSelection & {
24
+ __name?: string;
25
+ }) => GraphqlOperation;
@@ -0,0 +1,105 @@
1
+ import type { GraphqlOperation } from './generateGraphqlOperation';
2
+ type Variables = Record<string, any>;
3
+ type QueryError = Error & {
4
+ message: string;
5
+ locations?: Array<{
6
+ line: number;
7
+ column: number;
8
+ }>;
9
+ path?: any;
10
+ rid: string;
11
+ details?: Record<string, any>;
12
+ };
13
+ type Result = {
14
+ data: Record<string, any>;
15
+ errors: Array<QueryError>;
16
+ };
17
+ type Fetcher = (batchedQuery: GraphqlOperation | Array<GraphqlOperation>) => Promise<Array<Result>>;
18
+ type Options = {
19
+ batchInterval?: number;
20
+ shouldBatch?: boolean;
21
+ maxBatchSize?: number;
22
+ };
23
+ type Queue = Array<{
24
+ request: GraphqlOperation;
25
+ resolve: (...args: Array<any>) => any;
26
+ reject: (...args: Array<any>) => any;
27
+ }>;
28
+ /**
29
+ * Create a batcher client.
30
+ * @param {Fetcher} fetcher - A function that can handle the network requests to graphql endpoint
31
+ * @param {Options} options - the options to be used by client
32
+ * @param {boolean} options.shouldBatch - should the client batch requests. (default true)
33
+ * @param {integer} options.batchInterval - duration (in MS) of each batch window. (default 6)
34
+ * @param {integer} options.maxBatchSize - max number of requests in a batch. (default 0)
35
+ * @param {boolean} options.defaultHeaders - default headers to include with every request
36
+ *
37
+ * @example
38
+ * const fetcher = batchedQuery => fetch('path/to/graphql', {
39
+ * method: 'post',
40
+ * headers: {
41
+ * Accept: 'application/json',
42
+ * 'Content-Type': 'application/json',
43
+ * },
44
+ * body: JSON.stringify(batchedQuery),
45
+ * credentials: 'include',
46
+ * })
47
+ * .then(response => response.json())
48
+ *
49
+ * const client = new QueryBatcher(fetcher, { maxBatchSize: 10 })
50
+ */
51
+ export declare class QueryBatcher {
52
+ fetcher: Fetcher;
53
+ _options: Options;
54
+ _queue: Queue;
55
+ constructor(fetcher: Fetcher, { batchInterval, shouldBatch, maxBatchSize, }?: Options);
56
+ /**
57
+ * Fetch will send a graphql request and return the parsed json.
58
+ * @param {string} query - the graphql query.
59
+ * @param {Variables} variables - any variables you wish to inject as key/value pairs.
60
+ * @param {[string]} operationName - the graphql operationName.
61
+ * @param {Options} overrides - the client options overrides.
62
+ *
63
+ * @return {promise} resolves to parsed json of server response
64
+ *
65
+ * @example
66
+ * client.fetch(`
67
+ * query getHuman($id: ID!) {
68
+ * human(id: $id) {
69
+ * name
70
+ * height
71
+ * }
72
+ * }
73
+ * `, { id: "1001" }, 'getHuman')
74
+ * .then(human => {
75
+ * // do something with human
76
+ * console.log(human);
77
+ * });
78
+ */
79
+ fetch(query: string, variables?: Variables, operationName?: string, overrides?: Options): Promise<Result>;
80
+ /**
81
+ * Fetch will send a graphql request and return the parsed json.
82
+ * @param {string} query - the graphql query.
83
+ * @param {Variables} variables - any variables you wish to inject as key/value pairs.
84
+ * @param {[string]} operationName - the graphql operationName.
85
+ * @param {Options} overrides - the client options overrides.
86
+ *
87
+ * @return {Promise<Array<Result>>} resolves to parsed json of server response
88
+ *
89
+ * @example
90
+ * client.forceFetch(`
91
+ * query getHuman($id: ID!) {
92
+ * human(id: $id) {
93
+ * name
94
+ * height
95
+ * }
96
+ * }
97
+ * `, { id: "1001" }, 'getHuman')
98
+ * .then(human => {
99
+ * // do something with human
100
+ * console.log(human);
101
+ * });
102
+ */
103
+ forceFetch(query: string, variables?: Variables, operationName?: string, overrides?: Options): Promise<Result>;
104
+ }
105
+ export {};
@@ -0,0 +1,17 @@
1
+ import { type BatchOptions } from './fetcher';
2
+ import type { ExecutionResult, LinkedType } from './types';
3
+ import { type GraphqlOperation } from './generateGraphqlOperation';
4
+ export type Headers = HeadersInit | (() => HeadersInit) | (() => Promise<HeadersInit>);
5
+ export type BaseFetcher = (operation: GraphqlOperation | GraphqlOperation[]) => Promise<ExecutionResult | ExecutionResult[]>;
6
+ export type ClientOptions = Omit<RequestInit, 'body' | 'headers'> & {
7
+ url?: string;
8
+ batch?: BatchOptions | boolean;
9
+ fetcher?: BaseFetcher;
10
+ fetch?: Function;
11
+ headers?: Headers;
12
+ };
13
+ export declare const createClient: ({ queryRoot, mutationRoot, subscriptionRoot, ...options }: ClientOptions & {
14
+ queryRoot?: LinkedType;
15
+ mutationRoot?: LinkedType;
16
+ subscriptionRoot?: LinkedType;
17
+ }) => any;
@@ -0,0 +1,18 @@
1
+ export declare class GenqlError extends Error {
2
+ errors: Array<GraphqlError>;
3
+ /**
4
+ * Partial data returned by the server
5
+ */
6
+ data?: any;
7
+ constructor(errors: any[], data: any);
8
+ }
9
+ interface GraphqlError {
10
+ message: string;
11
+ locations?: Array<{
12
+ line: number;
13
+ column: number;
14
+ }>;
15
+ path?: string[];
16
+ extensions?: Record<string, any>;
17
+ }
18
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { ClientOptions } from './createClient';
2
+ import type { GraphqlOperation } from './generateGraphqlOperation';
3
+ export interface Fetcher {
4
+ (gql: GraphqlOperation): Promise<any>;
5
+ }
6
+ export type BatchOptions = {
7
+ batchInterval?: number;
8
+ maxBatchSize?: number;
9
+ };
10
+ export declare const createFetcher: ({ url, headers, fetcher, fetch: _fetch, batch, ...rest }: ClientOptions) => Fetcher;
@@ -0,0 +1,30 @@
1
+ import type { LinkedField, LinkedType } from './types';
2
+ export interface Args {
3
+ [arg: string]: any | undefined;
4
+ }
5
+ export interface Fields {
6
+ [field: string]: Request;
7
+ }
8
+ export type Request = boolean | number | Fields;
9
+ export interface Variables {
10
+ [name: string]: {
11
+ value: any;
12
+ typing: [LinkedType, string];
13
+ };
14
+ }
15
+ export interface Context {
16
+ root: LinkedType;
17
+ varCounter: number;
18
+ variables: Variables;
19
+ fragmentCounter: number;
20
+ fragments: string[];
21
+ }
22
+ export interface GraphqlOperation {
23
+ query: string;
24
+ variables?: {
25
+ [name: string]: any;
26
+ };
27
+ operationName?: string;
28
+ }
29
+ export declare const generateGraphqlOperation: (operation: "query" | "mutation" | "subscription", root: LinkedType, fields?: Fields) => GraphqlOperation;
30
+ export declare const getFieldFromPath: (root: LinkedType | undefined, path: string[]) => LinkedField;
@@ -0,0 +1,11 @@
1
+ export { createClient } from './createClient';
2
+ export type { ClientOptions } from './createClient';
3
+ export type { FieldsSelection } from './typeSelection';
4
+ export { generateGraphqlOperation } from './generateGraphqlOperation';
5
+ export type { GraphqlOperation } from './generateGraphqlOperation';
6
+ export { linkTypeMap } from './linkTypeMap';
7
+ export { createFetcher } from './fetcher';
8
+ export { GenqlError } from './error';
9
+ export declare const everything: {
10
+ __scalar: boolean;
11
+ };
@@ -0,0 +1,9 @@
1
+ import type { CompressedTypeMap, LinkedArgMap, LinkedTypeMap } from './types';
2
+ export interface PartialLinkedFieldMap {
3
+ [field: string]: {
4
+ type: string;
5
+ args?: LinkedArgMap;
6
+ };
7
+ }
8
+ export declare const linkTypeMap: (typeMap: CompressedTypeMap<number>) => LinkedTypeMap;
9
+ export declare const resolveConcreteTypes: (linkedTypeMap: LinkedTypeMap) => LinkedTypeMap;
@@ -0,0 +1,28 @@
1
+ export type FieldsSelection<SRC extends Anify<DST> | undefined, DST> = {
2
+ scalar: SRC;
3
+ union: Handle__isUnion<SRC, DST>;
4
+ object: HandleObject<SRC, DST>;
5
+ array: SRC extends Nil ? never : SRC extends Array<infer T | null> ? Array<FieldsSelection<T, DST>> : never;
6
+ __scalar: Handle__scalar<SRC, DST>;
7
+ never: never;
8
+ }[DST extends Nil ? 'never' : DST extends false | 0 ? 'never' : SRC extends Scalar ? 'scalar' : SRC extends any[] ? 'array' : SRC extends {
9
+ __isUnion?: any;
10
+ } ? 'union' : DST extends {
11
+ __scalar?: any;
12
+ } ? '__scalar' : DST extends {} ? 'object' : 'never'];
13
+ type HandleObject<SRC extends Anify<DST>, DST> = DST extends boolean ? SRC : SRC extends Nil ? never : Pick<{
14
+ [Key in keyof SRC]: Key extends keyof DST ? FieldsSelection<SRC[Key], NonNullable<DST[Key]>> : SRC[Key];
15
+ }, Exclude<keyof DST, FieldsToRemove>>;
16
+ type Handle__scalar<SRC extends Anify<DST>, DST> = SRC extends Nil ? never : Pick<{
17
+ [Key in keyof SRC]: Key extends keyof DST ? FieldsSelection<SRC[Key], DST[Key]> : SRC[Key];
18
+ }, {
19
+ [Key in keyof SRC]: SRC[Key] extends Nil ? never : Key extends FieldsToRemove ? never : SRC[Key] extends Scalar ? Key : Key extends keyof DST ? Key : never;
20
+ }[keyof SRC]>;
21
+ type Handle__isUnion<SRC extends Anify<DST>, DST> = SRC extends Nil ? never : Omit<SRC, FieldsToRemove>;
22
+ type Scalar = string | number | Date | boolean | null | undefined;
23
+ type Anify<T> = {
24
+ [P in keyof T]?: any;
25
+ };
26
+ type FieldsToRemove = '__isUnion' | '__scalar' | '__name' | '__args';
27
+ type Nil = undefined | null;
28
+ export {};
@@ -0,0 +1,55 @@
1
+ export interface ExecutionResult<TData = {
2
+ [key: string]: any;
3
+ }> {
4
+ errors?: Array<Error>;
5
+ data?: TData | null;
6
+ }
7
+ export interface ArgMap<keyType = number> {
8
+ [arg: string]: [keyType, string] | [keyType] | undefined;
9
+ }
10
+ export type CompressedField<keyType = number> = [
11
+ type: keyType,
12
+ args?: ArgMap<keyType>
13
+ ];
14
+ export interface CompressedFieldMap<keyType = number> {
15
+ [field: string]: CompressedField<keyType> | undefined;
16
+ }
17
+ export type CompressedType<keyType = number> = CompressedFieldMap<keyType>;
18
+ export interface CompressedTypeMap<keyType = number> {
19
+ scalars: Array<keyType>;
20
+ types: {
21
+ [type: string]: CompressedType<keyType> | undefined;
22
+ };
23
+ }
24
+ export type Field<keyType = number> = {
25
+ type: keyType;
26
+ args?: ArgMap<keyType>;
27
+ };
28
+ export interface FieldMap<keyType = number> {
29
+ [field: string]: Field<keyType> | undefined;
30
+ }
31
+ export type Type<keyType = number> = FieldMap<keyType>;
32
+ export interface TypeMap<keyType = number> {
33
+ scalars: Array<keyType>;
34
+ types: {
35
+ [type: string]: Type<keyType> | undefined;
36
+ };
37
+ }
38
+ export interface LinkedArgMap {
39
+ [arg: string]: [LinkedType, string] | undefined;
40
+ }
41
+ export interface LinkedField {
42
+ type: LinkedType;
43
+ args?: LinkedArgMap;
44
+ }
45
+ export interface LinkedFieldMap {
46
+ [field: string]: LinkedField | undefined;
47
+ }
48
+ export interface LinkedType {
49
+ name: string;
50
+ fields?: LinkedFieldMap;
51
+ scalar?: string[];
52
+ }
53
+ export interface LinkedTypeMap {
54
+ [type: string]: LinkedType | undefined;
55
+ }