twenty-sdk 0.6.0 → 0.6.2

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 (70) hide show
  1. package/README.md +6 -2
  2. package/dist/{HtmlTagToRemoteComponent-DzQ2I9Lz.js → HtmlTagToRemoteComponent-CgtwxDeH.js} +11 -11
  3. package/dist/{HtmlTagToRemoteComponent-DBOkvIRB.mjs → HtmlTagToRemoteComponent-D_EW5OLu.mjs} +1743 -1640
  4. package/dist/cli/utilities/api/api-service.d.ts +4 -0
  5. package/dist/cli/utilities/build/common/esbuild-watcher.d.ts +5 -2
  6. package/dist/cli/utilities/build/common/typecheck-plugin.d.ts +1 -1
  7. package/dist/cli/utilities/build/manifest/manifest-extract-config.d.ts +2 -0
  8. package/dist/cli/utilities/client/client-service.d.ts +5 -1
  9. package/dist/cli/utilities/client/twenty-client-template.d.ts +42 -0
  10. package/dist/cli/utilities/dev/orchestrator/dev-mode-orchestrator.d.ts +2 -0
  11. package/dist/cli/utilities/dev/orchestrator/steps/start-watchers-orchestrator-step.d.ts +2 -0
  12. package/dist/cli.cjs +430 -75
  13. package/dist/cli.mjs +3175 -2725
  14. package/dist/front-component-renderer/index.cjs +11 -11
  15. package/dist/front-component-renderer/index.d.ts +3 -3
  16. package/dist/front-component-renderer/index.mjs +2186 -1906
  17. package/dist/front-component-renderer/remote/generated/remote-components.d.ts +44 -0
  18. package/dist/front-component-renderer/remote/generated/remote-elements.d.ts +93 -0
  19. package/dist/front-component-renderer/types/FrontComponentHostCommunicationApi.d.ts +2 -1
  20. package/dist/{get-function-input-schema-BZ7_XyUh-GxSybvDe.js → get-function-input-schema-BZ7_XyUh-CCSi0u_q.js} +1 -1
  21. package/dist/{get-function-input-schema-BZ7_XyUh-By3UDh4s.mjs → get-function-input-schema-BZ7_XyUh-DAPandzB.mjs} +1 -1
  22. package/dist/index.cjs +2 -2
  23. package/dist/index.mjs +122 -89
  24. package/dist/sdk/application/application-config.d.ts +1 -1
  25. package/dist/sdk/front-component-api/constants/AllowedHtmlElements.d.ts +1 -0
  26. package/dist/sdk/front-component-api/constants/AllowedHtmlElements.js +104 -1
  27. package/dist/sdk/front-component-api/constants/AllowedHtmlElements.js.map +1 -1
  28. package/dist/sdk/front-component-api/constants/EventToReact.js +22 -3
  29. package/dist/sdk/front-component-api/constants/EventToReact.js.map +1 -1
  30. package/dist/sdk/front-component-api/constants/SerializedEventData.d.ts +7 -0
  31. package/dist/sdk/front-component-api/globals/frontComponentHostCommunicationApi.d.ts +2 -0
  32. package/dist/sdk/front-component-api/globals/frontComponentHostCommunicationApi.js.map +1 -1
  33. package/dist/sdk/index.d.ts +3 -0
  34. package/dist/sdk/index.js +70 -66
  35. package/dist/sdk/index.js.map +1 -1
  36. package/dist/sdk/logic-functions/define-post-install-logic-function.d.ts +6 -0
  37. package/dist/sdk/logic-functions/define-post-install-logic-function.js +12 -0
  38. package/dist/sdk/logic-functions/define-post-install-logic-function.js.map +1 -0
  39. package/dist/sdk/logic-functions/define-pre-install-logic-function.d.ts +6 -0
  40. package/dist/sdk/logic-functions/define-pre-install-logic-function.js +12 -0
  41. package/dist/sdk/logic-functions/define-pre-install-logic-function.js.map +1 -0
  42. package/dist/sdk/logic-functions/install-logic-function-payload-type.d.ts +4 -0
  43. package/generated/core/index.ts +406 -0
  44. package/generated/core/runtime/batcher.ts +265 -0
  45. package/generated/core/runtime/createClient.ts +68 -0
  46. package/generated/core/runtime/error.ts +29 -0
  47. package/generated/core/runtime/fetcher.ts +98 -0
  48. package/generated/core/runtime/generateGraphqlOperation.ts +225 -0
  49. package/generated/core/runtime/index.ts +13 -0
  50. package/generated/core/runtime/linkTypeMap.ts +139 -0
  51. package/generated/core/runtime/typeSelection.ts +98 -0
  52. package/generated/core/runtime/types.ts +69 -0
  53. package/generated/core/schema.graphql +36745 -0
  54. package/generated/core/schema.ts +39341 -0
  55. package/generated/core/types.ts +45860 -0
  56. package/generated/index.ts +2 -0
  57. package/generated/metadata/index.ts +469 -0
  58. package/generated/metadata/runtime/batcher.ts +265 -0
  59. package/generated/metadata/runtime/createClient.ts +68 -0
  60. package/generated/metadata/runtime/error.ts +29 -0
  61. package/generated/metadata/runtime/fetcher.ts +98 -0
  62. package/generated/metadata/runtime/generateGraphqlOperation.ts +225 -0
  63. package/generated/metadata/runtime/index.ts +13 -0
  64. package/generated/metadata/runtime/linkTypeMap.ts +139 -0
  65. package/generated/metadata/runtime/typeSelection.ts +98 -0
  66. package/generated/metadata/runtime/types.ts +69 -0
  67. package/generated/metadata/schema.graphql +3971 -0
  68. package/generated/metadata/schema.ts +8357 -0
  69. package/generated/metadata/types.ts +10044 -0
  70. package/package.json +17 -1
@@ -0,0 +1,68 @@
1
+ // @ts-nocheck
2
+
3
+ import { type BatchOptions, createFetcher } from './fetcher'
4
+ import type { ExecutionResult, LinkedType } from './types'
5
+ import {
6
+ generateGraphqlOperation,
7
+ type GraphqlOperation,
8
+ } from './generateGraphqlOperation'
9
+
10
+ export type Headers =
11
+ | HeadersInit
12
+ | (() => HeadersInit)
13
+ | (() => Promise<HeadersInit>)
14
+
15
+ export type BaseFetcher = (
16
+ operation: GraphqlOperation | GraphqlOperation[],
17
+ ) => Promise<ExecutionResult | ExecutionResult[]>
18
+
19
+ export type ClientOptions = Omit<RequestInit, 'body' | 'headers'> & {
20
+ url?: string
21
+ batch?: BatchOptions | boolean
22
+ fetcher?: BaseFetcher
23
+ fetch?: Function
24
+ headers?: Headers
25
+ }
26
+
27
+ export const createClient = ({
28
+ queryRoot,
29
+ mutationRoot,
30
+ subscriptionRoot,
31
+ ...options
32
+ }: ClientOptions & {
33
+ queryRoot?: LinkedType
34
+ mutationRoot?: LinkedType
35
+ subscriptionRoot?: LinkedType
36
+ }) => {
37
+ const fetcher = createFetcher(options)
38
+ const client: {
39
+ query?: Function
40
+ mutation?: Function
41
+ } = {}
42
+
43
+ if (queryRoot) {
44
+ client.query = (request: any) => {
45
+ if (!queryRoot) throw new Error('queryRoot argument is missing')
46
+
47
+ const resultPromise = fetcher(
48
+ generateGraphqlOperation('query', queryRoot, request),
49
+ )
50
+
51
+ return resultPromise
52
+ }
53
+ }
54
+ if (mutationRoot) {
55
+ client.mutation = (request: any) => {
56
+ if (!mutationRoot)
57
+ throw new Error('mutationRoot argument is missing')
58
+
59
+ const resultPromise = fetcher(
60
+ generateGraphqlOperation('mutation', mutationRoot, request),
61
+ )
62
+
63
+ return resultPromise
64
+ }
65
+ }
66
+
67
+ return client as any
68
+ }
@@ -0,0 +1,29 @@
1
+ // @ts-nocheck
2
+ export class GenqlError extends Error {
3
+ errors: Array<GraphqlError> = []
4
+ /**
5
+ * Partial data returned by the server
6
+ */
7
+ data?: any
8
+ constructor(errors: any[], data: any) {
9
+ let message = Array.isArray(errors)
10
+ ? errors.map((x) => x?.message || '').join('\n')
11
+ : ''
12
+ if (!message) {
13
+ message = 'GraphQL error'
14
+ }
15
+ super(message)
16
+ this.errors = errors
17
+ this.data = data
18
+ }
19
+ }
20
+
21
+ interface GraphqlError {
22
+ message: string
23
+ locations?: Array<{
24
+ line: number
25
+ column: number
26
+ }>
27
+ path?: string[]
28
+ extensions?: Record<string, any>
29
+ }
@@ -0,0 +1,98 @@
1
+ // @ts-nocheck
2
+ import { QueryBatcher } from './batcher'
3
+
4
+ import type { ClientOptions } from './createClient'
5
+ import type { GraphqlOperation } from './generateGraphqlOperation'
6
+ import { GenqlError } from './error'
7
+
8
+ export interface Fetcher {
9
+ (gql: GraphqlOperation): Promise<any>
10
+ }
11
+
12
+ export type BatchOptions = {
13
+ batchInterval?: number // ms
14
+ maxBatchSize?: number
15
+ }
16
+
17
+ const DEFAULT_BATCH_OPTIONS = {
18
+ maxBatchSize: 10,
19
+ batchInterval: 40,
20
+ }
21
+
22
+ export const createFetcher = ({
23
+ url,
24
+ headers = {},
25
+ fetcher,
26
+ fetch: _fetch,
27
+ batch = false,
28
+ ...rest
29
+ }: ClientOptions): Fetcher => {
30
+ if (!url && !fetcher) {
31
+ throw new Error('url or fetcher is required')
32
+ }
33
+ if (!fetcher) {
34
+ fetcher = async (body) => {
35
+ let headersObject =
36
+ typeof headers == 'function' ? await headers() : headers
37
+ headersObject = headersObject || {}
38
+ if (typeof fetch === 'undefined' && !_fetch) {
39
+ throw new Error(
40
+ 'Global `fetch` function is not available, pass a fetch polyfill to Genql `createClient`',
41
+ )
42
+ }
43
+ let fetchImpl = _fetch || fetch
44
+ const res = await fetchImpl(url!, {
45
+ headers: {
46
+ 'Content-Type': 'application/json',
47
+ ...headersObject,
48
+ },
49
+ method: 'POST',
50
+ body: JSON.stringify(body),
51
+ ...rest,
52
+ })
53
+ if (!res.ok) {
54
+ throw new Error(`${res.statusText}: ${await res.text()}`)
55
+ }
56
+ const json = await res.json()
57
+ return json
58
+ }
59
+ }
60
+
61
+ if (!batch) {
62
+ return async (body) => {
63
+ const json = await fetcher!(body)
64
+ if (Array.isArray(json)) {
65
+ return json.map((json) => {
66
+ if (json?.errors?.length) {
67
+ throw new GenqlError(json.errors || [], json.data)
68
+ }
69
+ return json.data
70
+ })
71
+ } else {
72
+ if (json?.errors?.length) {
73
+ throw new GenqlError(json.errors || [], json.data)
74
+ }
75
+ return json.data
76
+ }
77
+ }
78
+ }
79
+
80
+ const batcher = new QueryBatcher(
81
+ async (batchedQuery) => {
82
+ // console.log(batchedQuery) // [{ query: 'query{user{age}}', variables: {} }, ...]
83
+ const json = await fetcher!(batchedQuery)
84
+ return json as any
85
+ },
86
+ batch === true ? DEFAULT_BATCH_OPTIONS : batch,
87
+ )
88
+
89
+ return async ({ query, variables }) => {
90
+ const json = await batcher.fetch(query, variables)
91
+ if (json?.data) {
92
+ return json.data
93
+ }
94
+ throw new Error(
95
+ 'Genql batch fetcher returned unexpected result ' + JSON.stringify(json),
96
+ )
97
+ }
98
+ }
@@ -0,0 +1,225 @@
1
+ // @ts-nocheck
2
+ import type { LinkedField, LinkedType } from './types'
3
+
4
+ export interface Args {
5
+ [arg: string]: any | undefined
6
+ }
7
+
8
+ export interface Fields {
9
+ [field: string]: Request
10
+ }
11
+
12
+ export type Request = boolean | number | Fields
13
+
14
+ export interface Variables {
15
+ [name: string]: {
16
+ value: any
17
+ typing: [LinkedType, string]
18
+ }
19
+ }
20
+
21
+ export interface Context {
22
+ root: LinkedType
23
+ varCounter: number
24
+ variables: Variables
25
+ fragmentCounter: number
26
+ fragments: string[]
27
+ }
28
+
29
+ export interface GraphqlOperation {
30
+ query: string
31
+ variables?: { [name: string]: any }
32
+ operationName?: string
33
+ }
34
+
35
+ const parseRequest = (
36
+ request: Request | undefined,
37
+ ctx: Context,
38
+ path: string[],
39
+ ): string => {
40
+ if (typeof request === 'object' && '__args' in request) {
41
+ const args: any = request.__args
42
+ let fields: Request | undefined = { ...request }
43
+ delete fields.__args
44
+ const argNames = Object.keys(args)
45
+
46
+ if (argNames.length === 0) {
47
+ return parseRequest(fields, ctx, path)
48
+ }
49
+
50
+ const field = getFieldFromPath(ctx.root, path)
51
+
52
+ const argStrings = argNames.map((argName) => {
53
+ ctx.varCounter++
54
+ const varName = `v${ctx.varCounter}`
55
+
56
+ const typing = field.args && field.args[argName] // typeMap used here, .args
57
+
58
+ if (!typing) {
59
+ throw new Error(
60
+ `no typing defined for argument \`${argName}\` in path \`${path.join(
61
+ '.',
62
+ )}\``,
63
+ )
64
+ }
65
+
66
+ ctx.variables[varName] = {
67
+ value: args[argName],
68
+ typing,
69
+ }
70
+
71
+ return `${argName}:$${varName}`
72
+ })
73
+ return `(${argStrings})${parseRequest(fields, ctx, path)}`
74
+ } else if (typeof request === 'object' && Object.keys(request).length > 0) {
75
+ const fields = request
76
+ const fieldNames = Object.keys(fields).filter((k) => Boolean(fields[k]))
77
+
78
+ if (fieldNames.length === 0) {
79
+ throw new Error(
80
+ `field selection should not be empty: ${path.join('.')}`,
81
+ )
82
+ }
83
+
84
+ const type =
85
+ path.length > 0 ? getFieldFromPath(ctx.root, path).type : ctx.root
86
+ const scalarFields = type.scalar
87
+
88
+ let scalarFieldsFragment: string | undefined
89
+
90
+ if (fieldNames.includes('__scalar')) {
91
+ const falsyFieldNames = new Set(
92
+ Object.keys(fields).filter((k) => !Boolean(fields[k])),
93
+ )
94
+ if (scalarFields?.length) {
95
+ ctx.fragmentCounter++
96
+ scalarFieldsFragment = `f${ctx.fragmentCounter}`
97
+
98
+ ctx.fragments.push(
99
+ `fragment ${scalarFieldsFragment} on ${
100
+ type.name
101
+ }{${scalarFields
102
+ .filter((f) => !falsyFieldNames.has(f))
103
+ .join(',')}}`,
104
+ )
105
+ }
106
+ }
107
+
108
+ const fieldsSelection = fieldNames
109
+ .filter((f) => !['__scalar', '__name'].includes(f))
110
+ .map((f) => {
111
+ const parsed = parseRequest(fields[f], ctx, [...path, f])
112
+
113
+ if (f.startsWith('on_')) {
114
+ ctx.fragmentCounter++
115
+ const implementationFragment = `f${ctx.fragmentCounter}`
116
+
117
+ const typeMatch = f.match(/^on_(.+)/)
118
+
119
+ if (!typeMatch || !typeMatch[1])
120
+ throw new Error('match failed')
121
+
122
+ ctx.fragments.push(
123
+ `fragment ${implementationFragment} on ${typeMatch[1]}${parsed}`,
124
+ )
125
+
126
+ return `...${implementationFragment}`
127
+ } else {
128
+ return `${f}${parsed}`
129
+ }
130
+ })
131
+ .concat(scalarFieldsFragment ? [`...${scalarFieldsFragment}`] : [])
132
+ .join(',')
133
+
134
+ return `{${fieldsSelection}}`
135
+ } else {
136
+ return ''
137
+ }
138
+ }
139
+
140
+ export const generateGraphqlOperation = (
141
+ operation: 'query' | 'mutation' | 'subscription',
142
+ root: LinkedType,
143
+ fields?: Fields,
144
+ ): GraphqlOperation => {
145
+ const ctx: Context = {
146
+ root: root,
147
+ varCounter: 0,
148
+ variables: {},
149
+ fragmentCounter: 0,
150
+ fragments: [],
151
+ }
152
+ const result = parseRequest(fields, ctx, [])
153
+
154
+ const varNames = Object.keys(ctx.variables)
155
+
156
+ const varsString =
157
+ varNames.length > 0
158
+ ? `(${varNames.map((v) => {
159
+ const variableType = ctx.variables[v].typing[1]
160
+ return `$${v}:${variableType}`
161
+ })})`
162
+ : ''
163
+
164
+ const operationName = fields?.__name || ''
165
+
166
+ return {
167
+ query: [
168
+ `${operation} ${operationName}${varsString}${result}`,
169
+ ...ctx.fragments,
170
+ ].join(','),
171
+ variables: Object.keys(ctx.variables).reduce<{ [name: string]: any }>(
172
+ (r, v) => {
173
+ r[v] = ctx.variables[v].value
174
+ return r
175
+ },
176
+ {},
177
+ ),
178
+ ...(operationName ? { operationName: operationName.toString() } : {}),
179
+ }
180
+ }
181
+
182
+ export const getFieldFromPath = (
183
+ root: LinkedType | undefined,
184
+ path: string[],
185
+ ) => {
186
+ let current: LinkedField | undefined
187
+
188
+ if (!root) throw new Error('root type is not provided')
189
+
190
+ if (path.length === 0) throw new Error(`path is empty`)
191
+
192
+ path.forEach((f) => {
193
+ const type = current ? current.type : root
194
+
195
+ if (!type.fields)
196
+ throw new Error(`type \`${type.name}\` does not have fields`)
197
+
198
+ const possibleTypes = Object.keys(type.fields)
199
+ .filter((i) => i.startsWith('on_'))
200
+ .reduce(
201
+ (types, fieldName) => {
202
+ const field = type.fields && type.fields[fieldName]
203
+ if (field) types.push(field.type)
204
+ return types
205
+ },
206
+ [type],
207
+ )
208
+
209
+ let field: LinkedField | null = null
210
+
211
+ possibleTypes.forEach((type) => {
212
+ const found = type.fields && type.fields[f]
213
+ if (found) field = found
214
+ })
215
+
216
+ if (!field)
217
+ throw new Error(
218
+ `type \`${type.name}\` does not have a field \`${f}\``,
219
+ )
220
+
221
+ current = field
222
+ })
223
+
224
+ return current as LinkedField
225
+ }
@@ -0,0 +1,13 @@
1
+ // @ts-nocheck
2
+ export { createClient } from './createClient'
3
+ export type { ClientOptions } from './createClient'
4
+ export type { FieldsSelection } from './typeSelection'
5
+ export { generateGraphqlOperation } from './generateGraphqlOperation'
6
+ export type { GraphqlOperation } from './generateGraphqlOperation'
7
+ export { linkTypeMap } from './linkTypeMap'
8
+ // export { Observable } from 'zen-observable-ts'
9
+ export { createFetcher } from './fetcher'
10
+ export { GenqlError } from './error'
11
+ export const everything = {
12
+ __scalar: true,
13
+ }
@@ -0,0 +1,139 @@
1
+ // @ts-nocheck
2
+ import type {
3
+ CompressedType,
4
+ CompressedTypeMap,
5
+ LinkedArgMap,
6
+ LinkedField,
7
+ LinkedType,
8
+ LinkedTypeMap,
9
+ } from './types'
10
+
11
+ export interface PartialLinkedFieldMap {
12
+ [field: string]: {
13
+ type: string
14
+ args?: LinkedArgMap
15
+ }
16
+ }
17
+
18
+ export const linkTypeMap = (
19
+ typeMap: CompressedTypeMap<number>,
20
+ ): LinkedTypeMap => {
21
+ const indexToName: Record<number, string> = Object.assign(
22
+ {},
23
+ ...Object.keys(typeMap.types).map((k, i) => ({ [i]: k })),
24
+ )
25
+
26
+ let intermediaryTypeMap = Object.assign(
27
+ {},
28
+ ...Object.keys(typeMap.types || {}).map(
29
+ (k): Record<string, LinkedType> => {
30
+ const type: CompressedType = typeMap.types[k]!
31
+ const fields = type || {}
32
+ return {
33
+ [k]: {
34
+ name: k,
35
+ // type scalar properties
36
+ scalar: Object.keys(fields).filter((f) => {
37
+ const [type] = fields[f] || []
38
+ return type && typeMap.scalars.includes(type)
39
+ }),
40
+ // fields with corresponding `type` and `args`
41
+ fields: Object.assign(
42
+ {},
43
+ ...Object.keys(fields).map(
44
+ (f): PartialLinkedFieldMap => {
45
+ const [typeIndex, args] = fields[f] || []
46
+ if (typeIndex == null) {
47
+ return {}
48
+ }
49
+ return {
50
+ [f]: {
51
+ // replace index with type name
52
+ type: indexToName[typeIndex],
53
+ args: Object.assign(
54
+ {},
55
+ ...Object.keys(args || {}).map(
56
+ (k) => {
57
+ // if argTypeString == argTypeName, argTypeString is missing, need to readd it
58
+ if (!args || !args[k]) {
59
+ return
60
+ }
61
+ const [
62
+ argTypeName,
63
+ argTypeString,
64
+ ] = args[k] as any
65
+ return {
66
+ [k]: [
67
+ indexToName[
68
+ argTypeName
69
+ ],
70
+ argTypeString ||
71
+ indexToName[
72
+ argTypeName
73
+ ],
74
+ ],
75
+ }
76
+ },
77
+ ),
78
+ ),
79
+ },
80
+ }
81
+ },
82
+ ),
83
+ ),
84
+ },
85
+ }
86
+ },
87
+ ),
88
+ )
89
+ const res = resolveConcreteTypes(intermediaryTypeMap)
90
+ return res
91
+ }
92
+
93
+ // replace typename with concrete type
94
+ export const resolveConcreteTypes = (linkedTypeMap: LinkedTypeMap) => {
95
+ Object.keys(linkedTypeMap).forEach((typeNameFromKey) => {
96
+ const type: LinkedType = linkedTypeMap[typeNameFromKey]!
97
+ // type.name = typeNameFromKey
98
+ if (!type.fields) {
99
+ return
100
+ }
101
+
102
+ const fields = type.fields
103
+
104
+ Object.keys(fields).forEach((f) => {
105
+ const field: LinkedField = fields[f]!
106
+
107
+ if (field.args) {
108
+ const args = field.args
109
+ Object.keys(args).forEach((key) => {
110
+ const arg = args[key]
111
+
112
+ if (arg) {
113
+ const [typeName] = arg
114
+
115
+ if (typeof typeName === 'string') {
116
+ if (!linkedTypeMap[typeName]) {
117
+ linkedTypeMap[typeName] = { name: typeName }
118
+ }
119
+
120
+ arg[0] = linkedTypeMap[typeName]!
121
+ }
122
+ }
123
+ })
124
+ }
125
+
126
+ const typeName = field.type as LinkedType | string
127
+
128
+ if (typeof typeName === 'string') {
129
+ if (!linkedTypeMap[typeName]) {
130
+ linkedTypeMap[typeName] = { name: typeName }
131
+ }
132
+
133
+ field.type = linkedTypeMap[typeName]!
134
+ }
135
+ })
136
+ })
137
+
138
+ return linkedTypeMap
139
+ }
@@ -0,0 +1,98 @@
1
+ // @ts-nocheck
2
+ //////////////////////////////////////////////////
3
+
4
+ // SOME THINGS TO KNOW BEFORE DIVING IN
5
+ /*
6
+ 0. DST is the request type, SRC is the response type
7
+
8
+ 1. FieldsSelection uses an object because currently is impossible to make recursive types
9
+
10
+ 2. FieldsSelection is a recursive type that makes a type based on request type and fields
11
+
12
+ 3. HandleObject handles object types
13
+
14
+ 4. Handle__scalar adds all scalar properties excluding non scalar props
15
+ */
16
+
17
+ export type FieldsSelection<SRC extends Anify<DST> | undefined, DST> = {
18
+ scalar: SRC
19
+ union: Handle__isUnion<SRC, DST>
20
+ object: HandleObject<SRC, DST>
21
+ array: SRC extends Nil
22
+ ? never
23
+ : SRC extends (infer T)[]
24
+ ? Array<FieldsSelection<T, DST>>
25
+ : never
26
+ __scalar: Handle__scalar<SRC, DST>
27
+ never: never
28
+ }[DST extends Nil
29
+ ? 'never'
30
+ : SRC extends Nil
31
+ ? 'never'
32
+ : DST extends false | 0
33
+ ? 'never'
34
+ : SRC extends Scalar
35
+ ? 'scalar'
36
+ : SRC extends any[]
37
+ ? 'array'
38
+ : SRC extends { __isUnion?: any }
39
+ ? 'union'
40
+ : DST extends { __scalar?: any }
41
+ ? '__scalar'
42
+ : DST extends {}
43
+ ? 'object'
44
+ : 'never']
45
+
46
+ type HandleObject<SRC extends Anify<DST>, DST> = SRC extends Nil
47
+ ? never
48
+ : Pick<
49
+ {
50
+ // using keyof SRC to maintain ?: relations of SRC type
51
+ [Key in keyof SRC]: Key extends keyof DST
52
+ ? FieldsSelection<
53
+ NonNullable<SRC[Key]>,
54
+ NonNullable<DST[Key]>
55
+ >
56
+ : SRC[Key]
57
+ },
58
+ Exclude<keyof DST, FieldsToRemove>
59
+ // {
60
+ // // remove falsy values
61
+ // [Key in keyof DST]: DST[Key] extends false | 0 ? never : Key
62
+ // }[keyof DST]
63
+ >
64
+
65
+ type Handle__scalar<SRC extends Anify<DST>, DST> = SRC extends Nil
66
+ ? never
67
+ : Pick<
68
+ // continue processing fields that are in DST, directly pass SRC type if not in DST
69
+ {
70
+ [Key in keyof SRC]: Key extends keyof DST
71
+ ? FieldsSelection<SRC[Key], DST[Key]>
72
+ : SRC[Key]
73
+ },
74
+ // remove fields that are not scalars or are not in DST
75
+ {
76
+ [Key in keyof SRC]: SRC[Key] extends Nil
77
+ ? never
78
+ : Key extends FieldsToRemove
79
+ ? never
80
+ : SRC[Key] extends Scalar
81
+ ? Key
82
+ : Key extends keyof DST
83
+ ? Key
84
+ : never
85
+ }[keyof SRC]
86
+ >
87
+
88
+ type Handle__isUnion<SRC extends Anify<DST>, DST> = SRC extends Nil
89
+ ? never
90
+ : Omit<SRC, FieldsToRemove> // just return the union type
91
+
92
+ type Scalar = string | number | Date | boolean | null | undefined
93
+
94
+ type Anify<T> = { [P in keyof T]?: any }
95
+
96
+ type FieldsToRemove = '__isUnion' | '__scalar' | '__name' | '__args'
97
+
98
+ type Nil = undefined | null