Skip to content

Commit

Permalink
fix: add Date type as allowed simple key
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 19, 2022
1 parent c9fca55 commit 44fa46e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export type KeyWithOptionalPrefix<T, O extends string> = T extends string ? (`${
type PrevIndex = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

export type SimpleKeys<T extends Record<string, any>> =
{[Key in keyof T & (string | number)]: T[Key] extends Record<string, any>
? never
{[Key in keyof T & (string | number)]: Flatten<T[Key]> extends Record<string, any>
? (Flatten<T[Key]> extends Date ? `${Key}` : never)
: `${Key}`
}[keyof T & (string | number)];

export type NestedKeys<T extends Record<string, any>, Depth extends number = 4> =
[Depth] extends [0] ? never :
{[Key in keyof T & (string | number)]: Flatten<T[Key]> extends Record<string, any>
? `${Key}.${NestedKeys<Flatten<T[Key]>, PrevIndex[Depth]>}`
? (Flatten<T[Key]> extends Date ? `${Key}` : `${Key}.${NestedKeys<Flatten<T[Key]>, PrevIndex[Depth]>}`)
: `${Key}`
}[keyof T & (string | number)];

Expand Down
5 changes: 3 additions & 2 deletions test/unit/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('src/build.ts', () => {
type Entity = {
id: number,
name: string,
created_at: Date,
child: ChildEntity,
siblings: ChildEntity[]
};
Expand Down Expand Up @@ -194,10 +195,10 @@ describe('src/build.ts', () => {
expect(record).toEqual(buildURLQueryString({ fields: 'id' }));

record = buildQuery<Entity>({
fields: ['id', 'name'],
fields: ['id', 'name', 'created_at'],
});

expect(record).toEqual(buildURLQueryString({ fields: ['id', 'name'] }));
expect(record).toEqual(buildURLQueryString({ fields: ['id', 'name', 'created_at'] }));

record = buildQuery<Entity>({
fields: '+id',
Expand Down

0 comments on commit 44fa46e

Please sign in to comment.