-
-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathhelpers.ts
91 lines (81 loc) · 2.21 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// helpers.ts
import { Fetch, SupabaseClientOptions } from './types'
export function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
export function stripTrailingSlash(url: string): string {
return url.replace(/\/$/, '')
}
export const isBrowser = () => typeof window !== 'undefined'
export function applySettingDefaults<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
>(
options: SupabaseClientOptions<SchemaName>,
defaults: SupabaseClientOptions<any>
): Required<SupabaseClientOptions<SchemaName>> {
const {
db: dbOptions,
auth: authOptions,
realtime: realtimeOptions,
global: globalOptions,
} = options
const {
db: DEFAULT_DB_OPTIONS,
auth: DEFAULT_AUTH_OPTIONS,
realtime: DEFAULT_REALTIME_OPTIONS,
global: DEFAULT_GLOBAL_OPTIONS,
} = defaults
const result: Required<SupabaseClientOptions<SchemaName>> = {
db: {
...DEFAULT_DB_OPTIONS,
...dbOptions,
},
auth: {
...DEFAULT_AUTH_OPTIONS,
...authOptions,
},
realtime: {
...DEFAULT_REALTIME_OPTIONS,
...realtimeOptions,
},
global: {
...DEFAULT_GLOBAL_OPTIONS,
...globalOptions,
},
accessToken: async () => '',
}
if (options.accessToken) {
result.accessToken = options.accessToken
} else {
// hack around Required<>
delete (result as any).accessToken
}
return result
}
export const resolveFetch = (customFetch?: Fetch): Fetch => {
let _fetch: Fetch
if (customFetch) {
_fetch = customFetch
} else if (typeof fetch === 'undefined') {
_fetch = (...args) =>
import('@supabase/node-fetch' as any).then(({ default: fetch }) => fetch(...args))
} else {
_fetch = fetch
}
return (...args: Parameters<Fetch>) => _fetch(...args)
}
export const resolveHeadersConstructor = async () => {
if (typeof Headers === 'undefined') {
return import('@supabase/node-fetch' as any).then(
({ Headers: NodeFetchHeaders }) => NodeFetchHeaders as typeof Headers
)
}
return Headers
}