@shopify/hydrogen-codegen 0.3.1 → 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.
@@ -34,7 +34,7 @@ const caapiDefaultValues = {
34
34
  )
35
35
  };
36
36
  function getDefaultOptions(outputFile = "") {
37
- return /^(customer|caapi\.)/i.test(outputFile) ? caapiDefaultValues : sfapiDefaultValues;
37
+ return /(customer|caapi\.)/i.test(outputFile) ? caapiDefaultValues : sfapiDefaultValues;
38
38
  }
39
39
 
40
40
  exports.getDefaultOptions = getDefaultOptions;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/defaults.ts"],"names":[],"mappings":"AAEA,MAAM,sBAAsB;AAC5B,MAAM,wBAAwB;AAE9B,MAAM,qCAAqC;AAAA;AAAA,wCAEH,mBAAmB;AAAA,0CACjB,qBAAqB;AAAA;AAG/D,MAAM,qCAAqC;AAAA;AAAA,6CAEE,mBAAmB;AAAA,+CACjB,qBAAqB;AAAA;AAGpE,SAAS,oBACP,MACA,WACA,cACA;AACA,SAAO,KACJ,QAAQ,qBAAqB,SAAS,EACtC,QAAQ,uBAAuB,YAAY;AAChD;AAQA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEO,SAAS,kBAAkB,aAAa,IAAI;AACjD,SAAO,uBAAuB,KAAK,UAAU,IACzC,qBACA;AACN","sourcesContent":["import type {PresetConfig} from './preset';\n\nconst QUERIES_PLACEHOLDER = '%queries%';\nconst MUTATIONS_PLACEHOLDER = '%mutations%';\n\nconst sfapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${QUERIES_PLACEHOLDER} {}\n interface StorefrontMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nconst caapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface CustomerAccountQueries extends ${QUERIES_PLACEHOLDER} {}\n interface CustomerAccountMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nfunction replacePlaceholders(\n code: string,\n queryType: string,\n mutationType: string,\n) {\n return code\n .replace(QUERIES_PLACEHOLDER, queryType)\n .replace(MUTATIONS_PLACEHOLDER, mutationType);\n}\n\ntype DefaultValues = {\n importTypesFrom: string;\n namespacedImportName: string;\n interfaceExtensionCode: NonNullable<PresetConfig['interfaceExtension']>;\n};\n\nconst sfapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/storefront-api-types',\n namespacedImportName: 'StorefrontAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n sfapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nconst caapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/customer-account-api-types',\n namespacedImportName: 'CustomerAccountAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n caapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nexport function getDefaultOptions(outputFile = '') {\n return /^(customer|caapi\\.)/i.test(outputFile)\n ? caapiDefaultValues\n : sfapiDefaultValues;\n}\n"]}
1
+ {"version":3,"sources":["../../src/defaults.ts"],"names":[],"mappings":"AAEA,MAAM,sBAAsB;AAC5B,MAAM,wBAAwB;AAE9B,MAAM,qCAAqC;AAAA;AAAA,wCAEH,mBAAmB;AAAA,0CACjB,qBAAqB;AAAA;AAG/D,MAAM,qCAAqC;AAAA;AAAA,6CAEE,mBAAmB;AAAA,+CACjB,qBAAqB;AAAA;AAGpE,SAAS,oBACP,MACA,WACA,cACA;AACA,SAAO,KACJ,QAAQ,qBAAqB,SAAS,EACtC,QAAQ,uBAAuB,YAAY;AAChD;AAQA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEO,SAAS,kBAAkB,aAAa,IAAI;AACjD,SAAO,sBAAsB,KAAK,UAAU,IACxC,qBACA;AACN","sourcesContent":["import type {PresetConfig} from './preset';\n\nconst QUERIES_PLACEHOLDER = '%queries%';\nconst MUTATIONS_PLACEHOLDER = '%mutations%';\n\nconst sfapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${QUERIES_PLACEHOLDER} {}\n interface StorefrontMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nconst caapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface CustomerAccountQueries extends ${QUERIES_PLACEHOLDER} {}\n interface CustomerAccountMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nfunction replacePlaceholders(\n code: string,\n queryType: string,\n mutationType: string,\n) {\n return code\n .replace(QUERIES_PLACEHOLDER, queryType)\n .replace(MUTATIONS_PLACEHOLDER, mutationType);\n}\n\ntype DefaultValues = {\n importTypesFrom: string;\n namespacedImportName: string;\n interfaceExtensionCode: NonNullable<PresetConfig['interfaceExtension']>;\n};\n\nconst sfapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/storefront-api-types',\n namespacedImportName: 'StorefrontAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n sfapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nconst caapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/customer-account-api-types',\n namespacedImportName: 'CustomerAccountAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n caapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nexport function getDefaultOptions(outputFile = '') {\n return /(customer|caapi\\.)/i.test(outputFile)\n ? caapiDefaultValues\n : sfapiDefaultValues;\n}\n"]}
@@ -6,23 +6,23 @@ var graphqlCodegen = require('@shopify/graphql-codegen');
6
6
 
7
7
 
8
8
 
9
- Object.defineProperty(exports, 'preset', {
9
+ Object.defineProperty(exports, "preset", {
10
10
  enumerable: true,
11
11
  get: function () { return preset_js.preset; }
12
12
  });
13
- Object.defineProperty(exports, 'getSchema', {
13
+ Object.defineProperty(exports, "getSchema", {
14
14
  enumerable: true,
15
15
  get: function () { return schema_js.getSchema; }
16
16
  });
17
- Object.defineProperty(exports, 'pluckConfig', {
17
+ Object.defineProperty(exports, "pluckConfig", {
18
18
  enumerable: true,
19
19
  get: function () { return graphqlCodegen.pluckConfig; }
20
20
  });
21
- Object.defineProperty(exports, 'plugin', {
21
+ Object.defineProperty(exports, "plugin", {
22
22
  enumerable: true,
23
23
  get: function () { return graphqlCodegen.plugin; }
24
24
  });
25
- Object.defineProperty(exports, 'processSources', {
25
+ Object.defineProperty(exports, "processSources", {
26
26
  enumerable: true,
27
27
  get: function () { return graphqlCodegen.processSources; }
28
28
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,SAAQ,cAAgC;AACxC,SAAQ,iBAAgB;AACxB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAOK","sourcesContent":["export {preset, type PresetConfig} from './preset.js';\nexport {getSchema} from './schema.js';\nexport {\n plugin,\n pluckConfig,\n processSources,\n type GenericVariables,\n type EmptyVariables,\n type ClientReturn,\n type IsOptionalVariables,\n type ClientVariables,\n type ClientVariablesInRestParams,\n} from '@shopify/graphql-codegen';\n"]}
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"]}
@@ -32,7 +32,7 @@ const caapiDefaultValues = {
32
32
  )
33
33
  };
34
34
  function getDefaultOptions(outputFile = "") {
35
- return /^(customer|caapi\.)/i.test(outputFile) ? caapiDefaultValues : sfapiDefaultValues;
35
+ return /(customer|caapi\.)/i.test(outputFile) ? caapiDefaultValues : sfapiDefaultValues;
36
36
  }
37
37
 
38
38
  export { getDefaultOptions };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/defaults.ts"],"names":[],"mappings":"AAEA,MAAM,sBAAsB;AAC5B,MAAM,wBAAwB;AAE9B,MAAM,qCAAqC;AAAA;AAAA,wCAEH,mBAAmB;AAAA,0CACjB,qBAAqB;AAAA;AAG/D,MAAM,qCAAqC;AAAA;AAAA,6CAEE,mBAAmB;AAAA,+CACjB,qBAAqB;AAAA;AAGpE,SAAS,oBACP,MACA,WACA,cACA;AACA,SAAO,KACJ,QAAQ,qBAAqB,SAAS,EACtC,QAAQ,uBAAuB,YAAY;AAChD;AAQA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEO,SAAS,kBAAkB,aAAa,IAAI;AACjD,SAAO,uBAAuB,KAAK,UAAU,IACzC,qBACA;AACN","sourcesContent":["import type {PresetConfig} from './preset';\n\nconst QUERIES_PLACEHOLDER = '%queries%';\nconst MUTATIONS_PLACEHOLDER = '%mutations%';\n\nconst sfapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${QUERIES_PLACEHOLDER} {}\n interface StorefrontMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nconst caapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface CustomerAccountQueries extends ${QUERIES_PLACEHOLDER} {}\n interface CustomerAccountMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nfunction replacePlaceholders(\n code: string,\n queryType: string,\n mutationType: string,\n) {\n return code\n .replace(QUERIES_PLACEHOLDER, queryType)\n .replace(MUTATIONS_PLACEHOLDER, mutationType);\n}\n\ntype DefaultValues = {\n importTypesFrom: string;\n namespacedImportName: string;\n interfaceExtensionCode: NonNullable<PresetConfig['interfaceExtension']>;\n};\n\nconst sfapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/storefront-api-types',\n namespacedImportName: 'StorefrontAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n sfapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nconst caapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/customer-account-api-types',\n namespacedImportName: 'CustomerAccountAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n caapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nexport function getDefaultOptions(outputFile = '') {\n return /^(customer|caapi\\.)/i.test(outputFile)\n ? caapiDefaultValues\n : sfapiDefaultValues;\n}\n"]}
1
+ {"version":3,"sources":["../../src/defaults.ts"],"names":[],"mappings":"AAEA,MAAM,sBAAsB;AAC5B,MAAM,wBAAwB;AAE9B,MAAM,qCAAqC;AAAA;AAAA,wCAEH,mBAAmB;AAAA,0CACjB,qBAAqB;AAAA;AAG/D,MAAM,qCAAqC;AAAA;AAAA,6CAEE,mBAAmB;AAAA,+CACjB,qBAAqB;AAAA;AAGpE,SAAS,oBACP,MACA,WACA,cACA;AACA,SAAO,KACJ,QAAQ,qBAAqB,SAAS,EACtC,QAAQ,uBAAuB,YAAY;AAChD;AAQA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEO,SAAS,kBAAkB,aAAa,IAAI;AACjD,SAAO,sBAAsB,KAAK,UAAU,IACxC,qBACA;AACN","sourcesContent":["import type {PresetConfig} from './preset';\n\nconst QUERIES_PLACEHOLDER = '%queries%';\nconst MUTATIONS_PLACEHOLDER = '%mutations%';\n\nconst sfapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${QUERIES_PLACEHOLDER} {}\n interface StorefrontMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nconst caapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface CustomerAccountQueries extends ${QUERIES_PLACEHOLDER} {}\n interface CustomerAccountMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nfunction replacePlaceholders(\n code: string,\n queryType: string,\n mutationType: string,\n) {\n return code\n .replace(QUERIES_PLACEHOLDER, queryType)\n .replace(MUTATIONS_PLACEHOLDER, mutationType);\n}\n\ntype DefaultValues = {\n importTypesFrom: string;\n namespacedImportName: string;\n interfaceExtensionCode: NonNullable<PresetConfig['interfaceExtension']>;\n};\n\nconst sfapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/storefront-api-types',\n namespacedImportName: 'StorefrontAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n sfapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nconst caapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/customer-account-api-types',\n namespacedImportName: 'CustomerAccountAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n caapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nexport function getDefaultOptions(outputFile = '') {\n return /(customer|caapi\\.)/i.test(outputFile)\n ? caapiDefaultValues\n : sfapiDefaultValues;\n}\n"]}
@@ -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
@@ -159,7 +269,7 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
159
269
  ```
160
270
 
161
271
  @link https://github.com/microsoft/TypeScript/issues/15300
162
-
272
+ @see SimplifyDeep
163
273
  @category Object
164
274
  */
165
275
  export type Simplify<T> = {
@@ -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> = Simplify<
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<Pick<BaseType, Keys>>>;
394
+ Partial<HomomorphicPick<BaseType, Keys>>> : never;
244
395
  type PresetConfig = {
245
396
  /**
246
397
  * Whether types should be imported or generated inline.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,SAAQ,cAAgC;AACxC,SAAQ,iBAAgB;AACxB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAOK","sourcesContent":["export {preset, type PresetConfig} from './preset.js';\nexport {getSchema} from './schema.js';\nexport {\n plugin,\n pluckConfig,\n processSources,\n type GenericVariables,\n type EmptyVariables,\n type ClientReturn,\n type IsOptionalVariables,\n type ClientVariables,\n type ClientVariablesInRestParams,\n} from '@shopify/graphql-codegen';\n"]}
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.1",
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/shopify/hydrogen.git",
33
+ "url": "https://github.com/Shopify/hydrogen.git",
33
34
  "directory": "packages/hydrogen-codegen"
34
35
  },
35
36
  "files": [
@@ -39,7 +40,7 @@
39
40
  "@graphql-codegen/cli": "^5.0.1",
40
41
  "@graphql-codegen/plugin-helpers": "^5.0.2",
41
42
  "dts-bundle-generator": "^9.3.1",
42
- "type-fest": "^4.5.0",
43
+ "type-fest": "^4.26.1",
43
44
  "vitest": "^1.0.4"
44
45
  },
45
46
  "dependencies": {