From 95180b32574323179be871b326b785b7e3157d61 Mon Sep 17 00:00:00 2001 From: Satoshi Date: Mon, 22 Jan 2024 08:07:54 +0900 Subject: [PATCH] refactor --- src/index.ts | 76 +++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/src/index.ts b/src/index.ts index 327e006..c75f36b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,20 +1,26 @@ -type PocketBaseOption = { fields?: string; expand?: string; sort?: string; filter?: string } +type PocketBaseOption = { + fields?: string + expand?: string + sort?: string + filter?: string + requestKey?: string +} type BaseSchema = Record type BaseRelation = Record> -// prettier-ignore type WithEllipsis = '' | `${',' | ', '}${boolean}` type Modifier = `:excerpt(${number}${WithEllipsis})` -type RemoveModifier = T extends `${infer U}:${infer V}` ? U : T +type RemoveModifier = T extends `${infer U}:${string}` ? U : T +type FieldsArrayToUnion = T extends Array ? RemoveModifier : T type Option> = { [Key in keyof TSchema]: { key: Key fields?: (keyof { - [K in keyof TSchema[Key] & string as TSchema[Key][K] extends string - ? `${K}${'' | Modifier}` + [K in keyof TSchema[Key] as TSchema[Key][K] extends string + ? `${K & string}${'' | Modifier}` : K]: unknown })[] expand?: Expand>[] @@ -32,33 +38,22 @@ type Expand< > = { [Key in TKey]: { key: Key - fields?: Required[Key] extends Array + fields?: NonNullable extends Array | infer U ? (keyof { - [K in keyof U & string as U[K] extends string - ? `${K}${'' | Modifier}` - : K]: unknown - })[] - : (keyof { - [K in keyof TRelation[Key] & string as TRelation[Key][K] extends string - ? `${K}${'' | Modifier}` + [K in keyof U as U[K] extends string + ? `${K & string}${'' | Modifier}` : K]: unknown })[] - expand?: Required[Key] extends Array + : never + expand?: NonNullable extends Array ? Expand>[] - : Required[Key] extends infer U extends TSchema[keyof TSchema] + : NonNullable extends infer U extends TSchema[keyof TSchema] ? Expand>[] : never // the following is correct syntax and passes tests, but fails at build step // infer within union doesn't seem to work for whatever reason - // fields?: Required[Key] extends Array | infer U - // ? (keyof { - // [K in keyof U & string as U[K] extends string - // ? `${K}${'' | Modifier}` - // : K]: unknown - // })[] - // : never - // expand?: Required[Key] extends + // expand?: NonNullable extends // | Array // | infer U extends TSchema[keyof TSchema] // ? Expand>[] @@ -86,11 +81,10 @@ type ResponseType< TRelation extends BaseRelation, TOption extends Option, _Obj = TSchema[TOption['key']] -> = TOption['fields'] extends Array - ? RemoveModifier extends infer Fields extends keyof _Obj - ? Pick<_Obj, Fields> & ProcessExpandArray - : never - : _Obj & ProcessExpandArray +> = (FieldsArrayToUnion extends infer Fields extends keyof _Obj + ? Pick<_Obj, Fields> + : _Obj) & + ProcessExpandArray // check if all items in "expand" array are optional type AllOptional = { @@ -125,7 +119,7 @@ type ProcessExpandArray< } } : { - // if there's expand item that we know for sure isn't undefined, we don't need to +? on "expand" + // if there's expand item that we know for sure isn't undefined, we don't need to +? on "expand" itself expand: { // AFAIK, there's no way to add "?" modifier conditionally, so we have to use keys from TRelation [Key in keyof TRelation as Key extends TExpandArr[number]['key'] @@ -147,18 +141,14 @@ type ProcessSingleExpand< TSchema extends BaseSchema, TRelation extends BaseRelation, TExpand extends Expand, - _Obj = Required[TExpand['key']] extends Array | infer U ? U : never, - _IsToMany extends boolean = Required[TExpand['key']] extends Array - ? true - : false -> = TExpand['fields'] extends Array - ? RemoveModifier extends infer Fields extends keyof _Obj - ? HandleArray< - Pick<_Obj, Fields> & ProcessExpandArray, - _IsToMany - > - : never - : HandleArray<_Obj & ProcessExpandArray, _IsToMany> + _Obj = NonNullable extends Array | infer U ? U : never +> = HandleArray< + (FieldsArrayToUnion extends infer Fields extends keyof _Obj + ? Pick<_Obj, Fields> + : _Obj) & + ProcessExpandArray, + NonNullable extends Array ? true : false +> // Less "strict" version of Option so that we don't have to pass generics down to helper functions type HelperArg = { key: string; fields?: string[]; expand?: HelperArg[] } @@ -180,7 +170,7 @@ const getFields = (option: HelperArg, baseKey = ''): string => { if (JSON.stringify(expand).includes('"fields"')) { const expandFields = expand.map((exp) => { const fieldsAtDeeperLevel = getFields(exp, `${baseKey}expand.${exp.key}.`) - return `${fieldsAtDeeperLevel}` + return fieldsAtDeeperLevel }) return `${fieldsAtThisLevel},${expandFields.join(',')}` } @@ -230,5 +220,5 @@ export const initializeBuilder = } } - return [res, {} as ResponseType] + return [res, {} as any] }