@reqquest/ui 1.0.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.
Files changed (41) hide show
  1. package/README.md +3 -0
  2. package/dist/api.d.ts +595 -0
  3. package/dist/api.js +618 -0
  4. package/dist/components/ButtonLoadingIcon.svelte +27 -0
  5. package/dist/components/ButtonLoadingIcon.svelte.d.ts +18 -0
  6. package/dist/components/index.d.ts +1 -0
  7. package/dist/components/index.js +1 -0
  8. package/dist/index.d.ts +4 -0
  9. package/dist/index.js +4 -0
  10. package/dist/registry.d.ts +138 -0
  11. package/dist/registry.js +42 -0
  12. package/dist/stores/IStateStore.d.ts +5 -0
  13. package/dist/stores/IStateStore.js +1 -0
  14. package/dist/typed-client/index.d.ts +25 -0
  15. package/dist/typed-client/index.js +23 -0
  16. package/dist/typed-client/runtime/batcher.d.ts +105 -0
  17. package/dist/typed-client/runtime/batcher.js +203 -0
  18. package/dist/typed-client/runtime/createClient.d.ts +17 -0
  19. package/dist/typed-client/runtime/createClient.js +24 -0
  20. package/dist/typed-client/runtime/error.d.ts +18 -0
  21. package/dist/typed-client/runtime/error.js +19 -0
  22. package/dist/typed-client/runtime/fetcher.d.ts +10 -0
  23. package/dist/typed-client/runtime/fetcher.js +65 -0
  24. package/dist/typed-client/runtime/generateGraphqlOperation.d.ts +30 -0
  25. package/dist/typed-client/runtime/generateGraphqlOperation.js +128 -0
  26. package/dist/typed-client/runtime/index.d.ts +11 -0
  27. package/dist/typed-client/runtime/index.js +10 -0
  28. package/dist/typed-client/runtime/linkTypeMap.d.ts +9 -0
  29. package/dist/typed-client/runtime/linkTypeMap.js +95 -0
  30. package/dist/typed-client/runtime/typeSelection.d.ts +28 -0
  31. package/dist/typed-client/runtime/typeSelection.js +3 -0
  32. package/dist/typed-client/runtime/types.d.ts +55 -0
  33. package/dist/typed-client/runtime/types.js +2 -0
  34. package/dist/typed-client/schema.d.ts +1483 -0
  35. package/dist/typed-client/schema.graphql +1217 -0
  36. package/dist/typed-client/schema.js +313 -0
  37. package/dist/typed-client/types.d.ts +540 -0
  38. package/dist/typed-client/types.js +1368 -0
  39. package/dist/util.d.ts +2 -0
  40. package/dist/util.js +3 -0
  41. package/package.json +56 -0
@@ -0,0 +1,65 @@
1
+ // @ts-nocheck
2
+ import { QueryBatcher } from './batcher';
3
+ import { GenqlError } from './error';
4
+ const DEFAULT_BATCH_OPTIONS = {
5
+ maxBatchSize: 10,
6
+ batchInterval: 40,
7
+ };
8
+ export const createFetcher = ({ url, headers = {}, fetcher, fetch: _fetch, batch = false, ...rest }) => {
9
+ if (!url && !fetcher) {
10
+ throw new Error('url or fetcher is required');
11
+ }
12
+ fetcher = fetcher || (async (body) => {
13
+ let headersObject = typeof headers == 'function' ? await headers() : headers;
14
+ headersObject = headersObject || {};
15
+ if (typeof fetch === 'undefined' && !_fetch) {
16
+ throw new Error('Global `fetch` function is not available, pass a fetch polyfill to Genql `createClient`');
17
+ }
18
+ let fetchImpl = _fetch || fetch;
19
+ const res = await fetchImpl(url, {
20
+ headers: {
21
+ 'Content-Type': 'application/json',
22
+ ...headersObject,
23
+ },
24
+ method: 'POST',
25
+ body: JSON.stringify(body),
26
+ ...rest,
27
+ });
28
+ if (!res.ok) {
29
+ throw new Error(`${res.statusText}: ${await res.text()}`);
30
+ }
31
+ const json = await res.json();
32
+ return json;
33
+ });
34
+ if (!batch) {
35
+ return async (body) => {
36
+ const json = await fetcher(body);
37
+ if (Array.isArray(json)) {
38
+ return json.map((json) => {
39
+ if (json?.errors?.length) {
40
+ throw new GenqlError(json.errors || [], json.data);
41
+ }
42
+ return json.data;
43
+ });
44
+ }
45
+ else {
46
+ if (json?.errors?.length) {
47
+ throw new GenqlError(json.errors || [], json.data);
48
+ }
49
+ return json.data;
50
+ }
51
+ };
52
+ }
53
+ const batcher = new QueryBatcher(async (batchedQuery) => {
54
+ // console.log(batchedQuery) // [{ query: 'query{user{age}}', variables: {} }, ...]
55
+ const json = await fetcher(batchedQuery);
56
+ return json;
57
+ }, batch === true ? DEFAULT_BATCH_OPTIONS : batch);
58
+ return async ({ query, variables }) => {
59
+ const json = await batcher.fetch(query, variables);
60
+ if (json?.data) {
61
+ return json.data;
62
+ }
63
+ throw new Error('Genql batch fetcher returned unexpected result ' + JSON.stringify(json));
64
+ };
65
+ };
@@ -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,128 @@
1
+ const parseRequest = (request, ctx, path) => {
2
+ if (typeof request === 'object' && '__args' in request) {
3
+ const args = request.__args;
4
+ let fields = { ...request };
5
+ delete fields.__args;
6
+ const argNames = Object.keys(args);
7
+ if (argNames.length === 0) {
8
+ return parseRequest(fields, ctx, path);
9
+ }
10
+ const field = getFieldFromPath(ctx.root, path);
11
+ const argStrings = argNames.map((argName) => {
12
+ ctx.varCounter++;
13
+ const varName = `v${ctx.varCounter}`;
14
+ const typing = field.args && field.args[argName]; // typeMap used here, .args
15
+ if (!typing) {
16
+ throw new Error(`no typing defined for argument \`${argName}\` in path \`${path.join('.')}\``);
17
+ }
18
+ ctx.variables[varName] = {
19
+ value: args[argName],
20
+ typing,
21
+ };
22
+ return `${argName}:$${varName}`;
23
+ });
24
+ return `(${argStrings})${parseRequest(fields, ctx, path)}`;
25
+ }
26
+ else if (typeof request === 'object' && Object.keys(request).length > 0) {
27
+ const fields = request;
28
+ const fieldNames = Object.keys(fields).filter((k) => Boolean(fields[k]));
29
+ if (fieldNames.length === 0) {
30
+ throw new Error(`field selection should not be empty: ${path.join('.')}`);
31
+ }
32
+ const type = path.length > 0 ? getFieldFromPath(ctx.root, path).type : ctx.root;
33
+ const scalarFields = type.scalar;
34
+ let scalarFieldsFragment;
35
+ if (fieldNames.includes('__scalar')) {
36
+ const falsyFieldNames = new Set(Object.keys(fields).filter((k) => !Boolean(fields[k])));
37
+ if (scalarFields?.length) {
38
+ ctx.fragmentCounter++;
39
+ scalarFieldsFragment = `f${ctx.fragmentCounter}`;
40
+ ctx.fragments.push(`fragment ${scalarFieldsFragment} on ${type.name}{${scalarFields
41
+ .filter((f) => !falsyFieldNames.has(f))
42
+ .join(',')}}`);
43
+ }
44
+ }
45
+ const fieldsSelection = fieldNames
46
+ .filter((f) => !['__scalar', '__name'].includes(f))
47
+ .map((f) => {
48
+ const parsed = parseRequest(fields[f], ctx, [...path, f]);
49
+ if (f.startsWith('on_')) {
50
+ ctx.fragmentCounter++;
51
+ const implementationFragment = `f${ctx.fragmentCounter}`;
52
+ const typeMatch = f.match(/^on_(.+)/);
53
+ if (!typeMatch || !typeMatch[1])
54
+ throw new Error('match failed');
55
+ ctx.fragments.push(`fragment ${implementationFragment} on ${typeMatch[1]}${parsed}`);
56
+ return `...${implementationFragment}`;
57
+ }
58
+ else {
59
+ return `${f}${parsed}`;
60
+ }
61
+ })
62
+ .concat(scalarFieldsFragment ? [`...${scalarFieldsFragment}`] : [])
63
+ .join(',');
64
+ return `{${fieldsSelection}}`;
65
+ }
66
+ else {
67
+ return '';
68
+ }
69
+ };
70
+ export const generateGraphqlOperation = (operation, root, fields) => {
71
+ const ctx = {
72
+ root: root,
73
+ varCounter: 0,
74
+ variables: {},
75
+ fragmentCounter: 0,
76
+ fragments: [],
77
+ };
78
+ const result = parseRequest(fields, ctx, []);
79
+ const varNames = Object.keys(ctx.variables);
80
+ const varsString = varNames.length > 0
81
+ ? `(${varNames.map((v) => {
82
+ const variableType = ctx.variables[v].typing[1];
83
+ return `$${v}:${variableType}`;
84
+ })})`
85
+ : '';
86
+ const operationName = fields?.__name || '';
87
+ return {
88
+ query: [
89
+ `${operation} ${operationName}${varsString}${result}`,
90
+ ...ctx.fragments,
91
+ ].join(','),
92
+ variables: Object.keys(ctx.variables).reduce((r, v) => {
93
+ r[v] = ctx.variables[v].value;
94
+ return r;
95
+ }, {}),
96
+ ...(operationName ? { operationName: operationName.toString() } : {}),
97
+ };
98
+ };
99
+ export const getFieldFromPath = (root, path) => {
100
+ let current;
101
+ if (!root)
102
+ throw new Error('root type is not provided');
103
+ if (path.length === 0)
104
+ throw new Error(`path is empty`);
105
+ path.forEach((f) => {
106
+ const type = current ? current.type : root;
107
+ if (!type.fields)
108
+ throw new Error(`type \`${type.name}\` does not have fields`);
109
+ const possibleTypes = Object.keys(type.fields)
110
+ .filter((i) => i.startsWith('on_'))
111
+ .reduce((types, fieldName) => {
112
+ const field = type.fields && type.fields[fieldName];
113
+ if (field)
114
+ types.push(field.type);
115
+ return types;
116
+ }, [type]);
117
+ let field = null;
118
+ possibleTypes.forEach((type) => {
119
+ const found = type.fields && type.fields[f];
120
+ if (found)
121
+ field = found;
122
+ });
123
+ if (!field)
124
+ throw new Error(`type \`${type.name}\` does not have a field \`${f}\``);
125
+ current = field;
126
+ });
127
+ return current;
128
+ };
@@ -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,10 @@
1
+ // @ts-nocheck
2
+ export { createClient } from './createClient';
3
+ export { generateGraphqlOperation } from './generateGraphqlOperation';
4
+ export { linkTypeMap } from './linkTypeMap';
5
+ // export { Observable } from 'zen-observable-ts'
6
+ export { createFetcher } from './fetcher';
7
+ export { GenqlError } from './error';
8
+ export const everything = {
9
+ __scalar: true,
10
+ };
@@ -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,95 @@
1
+ export const linkTypeMap = (typeMap) => {
2
+ const indexToName = Object.assign({}, ...Object.keys(typeMap.types).map((k, i) => ({ [i]: k })));
3
+ let intermediaryTypeMap = Object.assign({}, ...Object.keys(typeMap.types || {}).map((k) => {
4
+ const type = typeMap.types[k];
5
+ const fields = type || {};
6
+ return {
7
+ [k]: {
8
+ name: k,
9
+ // type scalar properties
10
+ scalar: Object.keys(fields).filter((f) => {
11
+ const [type] = fields[f] || [];
12
+ const isScalar = type && typeMap.scalars.includes(type);
13
+ if (!isScalar) {
14
+ return false;
15
+ }
16
+ const args = fields[f]?.[1];
17
+ const argTypes = Object.values(args || {})
18
+ .map((x) => x?.[1])
19
+ .filter(Boolean);
20
+ const hasRequiredArgs = argTypes.some((str) => str && str.endsWith('!'));
21
+ if (hasRequiredArgs) {
22
+ return false;
23
+ }
24
+ return true;
25
+ }),
26
+ // fields with corresponding `type` and `args`
27
+ fields: Object.assign({}, ...Object.keys(fields).map((f) => {
28
+ const [typeIndex, args] = fields[f] || [];
29
+ if (typeIndex == null) {
30
+ return {};
31
+ }
32
+ return {
33
+ [f]: {
34
+ // replace index with type name
35
+ type: indexToName[typeIndex],
36
+ args: Object.assign({}, ...Object.keys(args || {}).map((k) => {
37
+ // if argTypeString == argTypeName, argTypeString is missing, need to readd it
38
+ if (!args || !args[k]) {
39
+ return;
40
+ }
41
+ const [argTypeName, argTypeString,] = args[k];
42
+ return {
43
+ [k]: [
44
+ indexToName[argTypeName],
45
+ argTypeString ||
46
+ indexToName[argTypeName],
47
+ ],
48
+ };
49
+ })),
50
+ },
51
+ };
52
+ })),
53
+ },
54
+ };
55
+ }));
56
+ const res = resolveConcreteTypes(intermediaryTypeMap);
57
+ return res;
58
+ };
59
+ // replace typename with concrete type
60
+ export const resolveConcreteTypes = (linkedTypeMap) => {
61
+ Object.keys(linkedTypeMap).forEach((typeNameFromKey) => {
62
+ const type = linkedTypeMap[typeNameFromKey];
63
+ // type.name = typeNameFromKey
64
+ if (!type.fields) {
65
+ return;
66
+ }
67
+ const fields = type.fields;
68
+ Object.keys(fields).forEach((f) => {
69
+ const field = fields[f];
70
+ if (field.args) {
71
+ const args = field.args;
72
+ Object.keys(args).forEach((key) => {
73
+ const arg = args[key];
74
+ if (arg) {
75
+ const [typeName] = arg;
76
+ if (typeof typeName === 'string') {
77
+ if (!linkedTypeMap[typeName]) {
78
+ linkedTypeMap[typeName] = { name: typeName };
79
+ }
80
+ arg[0] = linkedTypeMap[typeName];
81
+ }
82
+ }
83
+ });
84
+ }
85
+ const typeName = field.type;
86
+ if (typeof typeName === 'string') {
87
+ if (!linkedTypeMap[typeName]) {
88
+ linkedTypeMap[typeName] = { name: typeName };
89
+ }
90
+ field.type = linkedTypeMap[typeName];
91
+ }
92
+ });
93
+ });
94
+ return linkedTypeMap;
95
+ };
@@ -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,3 @@
1
+ // @ts-nocheck
2
+ //////////////////////////////////////////////////
3
+ 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
+ }
@@ -0,0 +1,2 @@
1
+ // @ts-nocheck
2
+ export {};