Skip to content

Commit 36b4cd5

Browse files
committed
refactor(postgrest): use postbuild to copy common types in supabase-js
1 parent 4b3ba3c commit 36b4cd5

File tree

3 files changed

+191
-2
lines changed

3 files changed

+191
-2
lines changed

packages/core/postgrest-js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"format": "node scripts/format.js",
3838
"format:check": "node scripts/format.js check",
3939
"build": "npm run clean && npm run build:cjs && npm run build:esm",
40+
"postbuild": "cp -rf ./src/types/common ../supabase-js/src/lib/rest/types/common",
4041
"build:cjs": "tsc -p tsconfig.json",
4142
"build:esm": "cpy wrapper.mjs dist/esm/",
4243
"docs": "typedoc src/index.ts --out docs/v2",

packages/core/supabase-js/src/lib/rest/types/common/common.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Types that are shared between supabase-js and postgrest-js
2+
3+
export type Fetch = typeof fetch
4+
5+
export type GenericRelationship = {
6+
foreignKeyName: string
7+
columns: string[]
8+
isOneToOne?: boolean
9+
referencedRelation: string
10+
referencedColumns: string[]
11+
}
12+
13+
export type GenericTable = {
14+
Row: Record<string, unknown>
15+
Insert: Record<string, unknown>
16+
Update: Record<string, unknown>
17+
Relationships: GenericRelationship[]
18+
}
19+
20+
export type GenericUpdatableView = {
21+
Row: Record<string, unknown>
22+
Insert: Record<string, unknown>
23+
Update: Record<string, unknown>
24+
Relationships: GenericRelationship[]
25+
}
26+
27+
export type GenericNonUpdatableView = {
28+
Row: Record<string, unknown>
29+
Relationships: GenericRelationship[]
30+
}
31+
32+
export type GenericView = GenericUpdatableView | GenericNonUpdatableView
33+
34+
export type GenericSetofOption = {
35+
isSetofReturn?: boolean | undefined
36+
isOneToOne?: boolean | undefined
37+
isNotNullable?: boolean | undefined
38+
to: string
39+
from: string
40+
}
41+
42+
export type GenericFunction = {
43+
Args: Record<string, unknown> | never
44+
Returns: unknown
45+
SetofOptions?: GenericSetofOption
46+
}
47+
48+
export type GenericSchema = {
49+
Tables: Record<string, GenericTable>
50+
Views: Record<string, GenericView>
51+
Functions: Record<string, GenericFunction>
52+
}
53+
54+
export type ClientServerOptions = {
55+
PostgrestVersion?: string
56+
}

packages/core/supabase-js/src/lib/rest/types/common/rpc.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import type { GenericFunction, GenericSchema, GenericSetofOption } from './common'
2+
3+
// Functions matching utils
4+
type IsMatchingArgs<
5+
FnArgs extends GenericFunction['Args'],
6+
PassedArgs extends GenericFunction['Args']
7+
> = [FnArgs] extends [Record<PropertyKey, never>]
8+
? PassedArgs extends Record<PropertyKey, never>
9+
? true
10+
: false
11+
: keyof PassedArgs extends keyof FnArgs
12+
? PassedArgs extends FnArgs
13+
? true
14+
: false
15+
: false
16+
17+
type MatchingFunctionArgs<
18+
Fn extends GenericFunction,
19+
Args extends GenericFunction['Args']
20+
> = Fn extends { Args: infer A extends GenericFunction['Args'] }
21+
? IsMatchingArgs<A, Args> extends true
22+
? Fn
23+
: never
24+
: false
25+
26+
type FindMatchingFunctionByArgs<
27+
FnUnion,
28+
Args extends GenericFunction['Args']
29+
> = FnUnion extends infer Fn extends GenericFunction ? MatchingFunctionArgs<Fn, Args> : false
30+
31+
// Types for working with database schemas
32+
type TablesAndViews<Schema extends GenericSchema> = Schema['Tables'] & Exclude<Schema['Views'], ''>
33+
34+
// Utility types for working with unions
35+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
36+
? I
37+
: never
38+
39+
type LastOf<T> = UnionToIntersection<T extends any ? () => T : never> extends () => infer R
40+
? R
41+
: never
42+
43+
type IsAny<T> = 0 extends 1 & T ? true : false
44+
45+
type ExactMatch<T, S> = [T] extends [S] ? ([S] extends [T] ? true : false) : false
46+
47+
type ExtractExactFunction<Fns, Args> = Fns extends infer F
48+
? F extends GenericFunction
49+
? ExactMatch<F['Args'], Args> extends true
50+
? F
51+
: never
52+
: never
53+
: never
54+
55+
type IsNever<T> = [T] extends [never] ? true : false
56+
57+
type RpcFunctionNotFound<FnName> = {
58+
Row: any
59+
Result: {
60+
error: true
61+
} & "Couldn't infer function definition matching provided arguments"
62+
RelationName: FnName
63+
Relationships: null
64+
}
65+
66+
export type GetRpcFunctionFilterBuilderByArgs<
67+
Schema extends GenericSchema,
68+
FnName extends string & keyof Schema['Functions'],
69+
Args
70+
> = {
71+
0: Schema['Functions'][FnName]
72+
// If the Args is exactly never (function call without any params)
73+
1: IsAny<Schema> extends true
74+
? any
75+
: IsNever<Args> extends true
76+
? // This is for retro compatibility, if the funcition is defined with an single return and an union of Args
77+
// we fallback to the last function definition matched by name
78+
IsNever<ExtractExactFunction<Schema['Functions'][FnName], Args>> extends true
79+
? LastOf<Schema['Functions'][FnName]>
80+
: ExtractExactFunction<Schema['Functions'][FnName], Args>
81+
: Args extends Record<PropertyKey, never>
82+
? LastOf<Schema['Functions'][FnName]>
83+
: // Otherwise, we attempt to match with one of the function definition in the union based
84+
// on the function arguments provided
85+
Args extends GenericFunction['Args']
86+
? // This is for retro compatibility, if the funcition is defined with an single return and an union of Args
87+
// we fallback to the last function definition matched by name
88+
IsNever<LastOf<FindMatchingFunctionByArgs<Schema['Functions'][FnName], Args>>> extends true
89+
? LastOf<Schema['Functions'][FnName]>
90+
: // Otherwise, we use the arguments based function definition narrowing to get the right value
91+
LastOf<FindMatchingFunctionByArgs<Schema['Functions'][FnName], Args>>
92+
: // If we can't find a matching function by args, we try to find one by function name
93+
ExtractExactFunction<Schema['Functions'][FnName], Args> extends GenericFunction
94+
? ExtractExactFunction<Schema['Functions'][FnName], Args>
95+
: any
96+
}[1] extends infer Fn
97+
? // If we are dealing with an non-typed client everything is any
98+
IsAny<Fn> extends true
99+
? { Row: any; Result: any; RelationName: FnName; Relationships: null }
100+
: // Otherwise, we use the arguments based function definition narrowing to get the rigt value
101+
Fn extends GenericFunction
102+
? {
103+
Row: Fn['SetofOptions'] extends GenericSetofOption
104+
? Fn['SetofOptions']['isSetofReturn'] extends true
105+
? TablesAndViews<Schema>[Fn['SetofOptions']['to']]['Row']
106+
: TablesAndViews<Schema>[Fn['SetofOptions']['to']]['Row']
107+
: Fn['Returns'] extends any[]
108+
? Fn['Returns'][number] extends Record<string, unknown>
109+
? Fn['Returns'][number]
110+
: never
111+
: Fn['Returns'] extends Record<string, unknown>
112+
? Fn['Returns']
113+
: never
114+
Result: Fn['SetofOptions'] extends GenericSetofOption
115+
? Fn['SetofOptions']['isSetofReturn'] extends true
116+
? Fn['SetofOptions']['isOneToOne'] extends true
117+
? Fn['Returns'][]
118+
: Fn['Returns']
119+
: Fn['Returns']
120+
: Fn['Returns']
121+
RelationName: Fn['SetofOptions'] extends GenericSetofOption
122+
? Fn['SetofOptions']['to']
123+
: FnName
124+
Relationships: Fn['SetofOptions'] extends GenericSetofOption
125+
? Fn['SetofOptions']['to'] extends keyof Schema['Tables']
126+
? Schema['Tables'][Fn['SetofOptions']['to']]['Relationships']
127+
: Schema['Views'][Fn['SetofOptions']['to']]['Relationships']
128+
: null
129+
}
130+
: // If we failed to find the function by argument, we still pass with any but also add an overridable
131+
Fn extends false
132+
? RpcFunctionNotFound<FnName>
133+
: RpcFunctionNotFound<FnName>
134+
: RpcFunctionNotFound<FnName>

0 commit comments

Comments
 (0)