@shopify/hydrogen-codegen 0.3.2 → 0.3.3
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/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.d.ts +154 -3
- package/dist/esm/index.js.map +1 -1
- package/package.json +3 -2
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,SAAQ,cAAgC;AACxC,SAAQ,iBAAgB;AACxB;AAAA,
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,SAAQ,cAAgC;AACxC,SAAQ,iBAAgB;AACxB;AAAA,EAOE;AAAA,EACA;AAAA,EACA;AAAA,OACK","sourcesContent":["export {preset, type PresetConfig} from './preset.js';\nexport {getSchema} from './schema.js';\nexport {\n type ClientReturn,\n type ClientVariables,\n type ClientVariablesInRestParams,\n type EmptyVariables,\n type GenericVariables,\n type IsOptionalVariables,\n pluckConfig,\n plugin,\n processSources,\n} from '@shopify/graphql-codegen';\n"]}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,6 +2,96 @@ import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
|
|
|
2
2
|
import { Source } from '@graphql-tools/utils';
|
|
3
3
|
import { ExecutionArgs, FragmentDefinitionNode, OperationDefinitionNode } from 'graphql';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
7
|
+
|
|
8
|
+
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
15
|
+
|
|
16
|
+
type Intersection = UnionToIntersection<Union>;
|
|
17
|
+
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
A more applicable example which could make its way into your library code follows.
|
|
21
|
+
|
|
22
|
+
@example
|
|
23
|
+
```
|
|
24
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
25
|
+
|
|
26
|
+
class CommandOne {
|
|
27
|
+
commands: {
|
|
28
|
+
a1: () => undefined,
|
|
29
|
+
b1: () => undefined,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
class CommandTwo {
|
|
34
|
+
commands: {
|
|
35
|
+
a2: (argA: string) => undefined,
|
|
36
|
+
b2: (argB: string) => undefined,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
|
|
41
|
+
type Union = typeof union;
|
|
42
|
+
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
|
|
43
|
+
|
|
44
|
+
type Intersection = UnionToIntersection<Union>;
|
|
45
|
+
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
@category Type
|
|
49
|
+
*/
|
|
50
|
+
export type UnionToIntersection<Union> = (
|
|
51
|
+
// `extends unknown` is always going to be the case and is used to convert the
|
|
52
|
+
// `Union` into a [distributive conditional
|
|
53
|
+
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
54
|
+
Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
|
|
55
|
+
/**
|
|
56
|
+
Create a union of all keys from a given type, even those exclusive to specific union members.
|
|
57
|
+
|
|
58
|
+
Unlike the native `keyof` keyword, which returns keys present in **all** union members, this type returns keys from **any** member.
|
|
59
|
+
|
|
60
|
+
@link https://stackoverflow.com/a/49402091
|
|
61
|
+
|
|
62
|
+
@example
|
|
63
|
+
```
|
|
64
|
+
import type {KeysOfUnion} from 'type-fest';
|
|
65
|
+
|
|
66
|
+
type A = {
|
|
67
|
+
common: string;
|
|
68
|
+
a: number;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
type B = {
|
|
72
|
+
common: string;
|
|
73
|
+
b: string;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
type C = {
|
|
77
|
+
common: string;
|
|
78
|
+
c: boolean;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
type Union = A | B | C;
|
|
82
|
+
|
|
83
|
+
type CommonKeys = keyof Union;
|
|
84
|
+
//=> 'common'
|
|
85
|
+
|
|
86
|
+
type AllKeys = KeysOfUnion<Union>;
|
|
87
|
+
//=> 'common' | 'a' | 'b' | 'c'
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
@category Object
|
|
91
|
+
*/
|
|
92
|
+
export type KeysOfUnion<ObjectType> =
|
|
93
|
+
// Hack to fix https://github.com/sindresorhus/type-fest/issues/1008
|
|
94
|
+
keyof UnionToIntersection<ObjectType extends unknown ? Record<keyof ObjectType, never> : never>;
|
|
5
95
|
/**
|
|
6
96
|
Returns a boolean for whether the two given types are equal.
|
|
7
97
|
|
|
@@ -28,7 +118,7 @@ type Includes<Value extends readonly any[], Item> =
|
|
|
28
118
|
@category Type Guard
|
|
29
119
|
@category Utilities
|
|
30
120
|
*/
|
|
31
|
-
export type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends (<G>() => G extends B ? 1 : 2) ? true : false;
|
|
121
|
+
export type IsEqual<A, B> = (<G>() => G extends A & G | G ? 1 : 2) extends (<G>() => G extends B & G | G ? 1 : 2) ? true : false;
|
|
32
122
|
/**
|
|
33
123
|
Filter out keys from an object.
|
|
34
124
|
|
|
@@ -96,6 +186,26 @@ type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
|
|
|
96
186
|
|
|
97
187
|
const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
|
|
98
188
|
//=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
|
|
189
|
+
|
|
190
|
+
// The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
|
|
191
|
+
|
|
192
|
+
// Consider the following example:
|
|
193
|
+
|
|
194
|
+
type UserData = {
|
|
195
|
+
[metadata: string]: string;
|
|
196
|
+
email: string;
|
|
197
|
+
name: string;
|
|
198
|
+
role: 'admin' | 'user';
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// `Omit` clearly doesn't behave as expected in this case:
|
|
202
|
+
type PostPayload = Omit<UserData, 'email'>;
|
|
203
|
+
//=> type PostPayload = { [x: string]: string; [x: number]: string; }
|
|
204
|
+
|
|
205
|
+
// In situations like this, `Except` works better.
|
|
206
|
+
// It simply removes the `email` key while preserving all the other keys.
|
|
207
|
+
type PostPayload = Except<UserData, 'email'>;
|
|
208
|
+
//=> type PostPayload = { [x: string]: string; name: string; role: 'admin' | 'user'; }
|
|
99
209
|
```
|
|
100
210
|
|
|
101
211
|
@category Object
|
|
@@ -212,6 +322,46 @@ export type IsNever<T> = [
|
|
|
212
322
|
never
|
|
213
323
|
] ? true : false;
|
|
214
324
|
/**
|
|
325
|
+
Works similar to the built-in `Pick` utility type, except for the following differences:
|
|
326
|
+
- Distributes over union types and allows picking keys from any member of the union type.
|
|
327
|
+
- Primitives types are returned as-is.
|
|
328
|
+
- Picks all keys if `Keys` is `any`.
|
|
329
|
+
- Doesn't pick `number` from a `string` index signature.
|
|
330
|
+
|
|
331
|
+
@example
|
|
332
|
+
```
|
|
333
|
+
type ImageUpload = {
|
|
334
|
+
url: string;
|
|
335
|
+
size: number;
|
|
336
|
+
thumbnailUrl: string;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
type VideoUpload = {
|
|
340
|
+
url: string;
|
|
341
|
+
duration: number;
|
|
342
|
+
encodingFormat: string;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
// Distributes over union types and allows picking keys from any member of the union type
|
|
346
|
+
type MediaDisplay = HomomorphicPick<ImageUpload | VideoUpload, "url" | "size" | "duration">;
|
|
347
|
+
//=> {url: string; size: number} | {url: string; duration: number}
|
|
348
|
+
|
|
349
|
+
// Primitive types are returned as-is
|
|
350
|
+
type Primitive = HomomorphicPick<string | number, 'toUpperCase' | 'toString'>;
|
|
351
|
+
//=> string | number
|
|
352
|
+
|
|
353
|
+
// Picks all keys if `Keys` is `any`
|
|
354
|
+
type Any = HomomorphicPick<{a: 1; b: 2} | {c: 3}, any>;
|
|
355
|
+
//=> {a: 1; b: 2} | {c: 3}
|
|
356
|
+
|
|
357
|
+
// Doesn't pick `number` from a `string` index signature
|
|
358
|
+
type IndexSignature = HomomorphicPick<{[k: string]: unknown}, number>;
|
|
359
|
+
//=> {}
|
|
360
|
+
*/
|
|
361
|
+
export type HomomorphicPick<T, Keys extends KeysOfUnion<T>> = {
|
|
362
|
+
[P in keyof T as Extract<P, Keys>]: T[P];
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
215
365
|
Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
|
|
216
366
|
|
|
217
367
|
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are optional.
|
|
@@ -236,11 +386,12 @@ type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
|
|
|
236
386
|
|
|
237
387
|
@category Object
|
|
238
388
|
*/
|
|
239
|
-
export type SetOptional<BaseType, Keys extends keyof BaseType> =
|
|
389
|
+
export type SetOptional<BaseType, Keys extends keyof BaseType> = BaseType extends unknown // To distribute `BaseType` when it's a union type.
|
|
390
|
+
? Simplify<
|
|
240
391
|
// Pick just the keys that are readonly from the base type.
|
|
241
392
|
Except<BaseType, Keys> &
|
|
242
393
|
// Pick the keys that should be mutable from the base type and make them mutable.
|
|
243
|
-
Partial<
|
|
394
|
+
Partial<HomomorphicPick<BaseType, Keys>>> : never;
|
|
244
395
|
type PresetConfig = {
|
|
245
396
|
/**
|
|
246
397
|
* Whether types should be imported or generated inline.
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,SAAQ,cAAgC;AACxC,SAAQ,iBAAgB;AACxB;AAAA,
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,SAAQ,cAAgC;AACxC,SAAQ,iBAAgB;AACxB;AAAA,EAOE;AAAA,EACA;AAAA,EACA;AAAA,OACK","sourcesContent":["export {preset, type PresetConfig} from './preset.js';\nexport {getSchema} from './schema.js';\nexport {\n type ClientReturn,\n type ClientVariables,\n type ClientVariablesInRestParams,\n type EmptyVariables,\n type GenericVariables,\n type IsOptionalVariables,\n pluckConfig,\n plugin,\n processSources,\n} from '@shopify/graphql-codegen';\n"]}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shopify/hydrogen-codegen",
|
|
3
|
+
"description": "A codegen plugin and preset for generating TypeScript types from GraphQL queries in a `d.ts` file.",
|
|
3
4
|
"publishConfig": {
|
|
4
5
|
"access": "public",
|
|
5
6
|
"@shopify:registry": "https://registry.npmjs.org"
|
|
6
7
|
},
|
|
7
|
-
"version": "0.3.
|
|
8
|
+
"version": "0.3.3",
|
|
8
9
|
"license": "MIT",
|
|
9
10
|
"type": "module",
|
|
10
11
|
"main": "dist/cjs/index.cjs",
|
|
@@ -29,7 +30,7 @@
|
|
|
29
30
|
},
|
|
30
31
|
"repository": {
|
|
31
32
|
"type": "git",
|
|
32
|
-
"url": "https://github.com/
|
|
33
|
+
"url": "https://github.com/Shopify/hydrogen.git",
|
|
33
34
|
"directory": "packages/hydrogen-codegen"
|
|
34
35
|
},
|
|
35
36
|
"files": [
|