@rebasepro/client 0.0.1-canary.4d4fb3e → 0.0.1-canary.ca2cb6e
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.
- package/dist/admin.d.ts +6 -6
- package/dist/auth.d.ts +67 -1
- package/dist/collection.d.ts +3 -3
- package/dist/cron.d.ts +25 -0
- package/dist/cron.test.d.ts +1 -0
- package/dist/index.d.ts +12 -7
- package/dist/index.es.js +523 -128
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +522 -129
- package/dist/index.umd.js.map +1 -1
- package/dist/query_builder.d.ts +3 -3
- package/dist/reviver.d.ts +1 -0
- package/dist/transport.d.ts +5 -5
- package/dist/websocket.d.ts +16 -10
- package/package.json +78 -74
- package/src/admin.ts +12 -12
- package/src/auth.ts +137 -25
- package/src/collection.ts +130 -79
- package/src/cron.test.ts +164 -0
- package/src/cron.ts +62 -0
- package/src/index.ts +39 -26
- package/src/query_builder.ts +7 -8
- package/src/reviver.ts +39 -0
- package/src/storage.ts +38 -38
- package/src/transport.ts +109 -23
- package/src/websocket.ts +104 -42
package/dist/query_builder.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { FindResponse } from "
|
|
1
|
+
import { FindResponse } from "@rebasepro/types";
|
|
2
2
|
import { CollectionClient } from "./collection";
|
|
3
3
|
export type FilterOperator = "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "in" | "nin" | "cs" | "csa" | "==" | "!=" | ">" | ">=" | "<" | "<=" | "array-contains" | "array-contains-any" | "not-in";
|
|
4
|
-
export declare class QueryBuilder<M extends Record<string,
|
|
4
|
+
export declare class QueryBuilder<M extends Record<string, unknown> = Record<string, unknown>> {
|
|
5
5
|
private collection;
|
|
6
6
|
private params;
|
|
7
7
|
constructor(collection: CollectionClient<M>);
|
|
@@ -10,7 +10,7 @@ export declare class QueryBuilder<M extends Record<string, any> = any> {
|
|
|
10
10
|
* @example
|
|
11
11
|
* client.collection('users').where('age', '>=', 18).find()
|
|
12
12
|
*/
|
|
13
|
-
where(column: keyof M & string, operator: FilterOperator, value:
|
|
13
|
+
where(column: keyof M & string, operator: FilterOperator, value: unknown): this;
|
|
14
14
|
/**
|
|
15
15
|
* Order the results by a specific column.
|
|
16
16
|
* @example
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function rebaseReviver(_key: string, value: unknown): unknown;
|
package/dist/transport.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FindParams as TypesFindParams, FindResponse as TypesFindResponse } from "@rebasepro/types";
|
|
2
2
|
export interface RebaseClientConfig {
|
|
3
|
-
baseUrl
|
|
3
|
+
baseUrl?: string;
|
|
4
4
|
token?: string;
|
|
5
5
|
apiPath?: string;
|
|
6
6
|
fetch?: typeof globalThis.fetch;
|
|
@@ -11,16 +11,16 @@ export interface RebaseClientConfig {
|
|
|
11
11
|
* Re-export from `@rebasepro/types` for backward compatibility.
|
|
12
12
|
*/
|
|
13
13
|
export type FindParams = TypesFindParams;
|
|
14
|
-
export type FindResponse<T> = TypesFindResponse<T extends Record<string,
|
|
14
|
+
export type FindResponse<T> = TypesFindResponse<T extends Record<string, unknown> ? T : Record<string, unknown>>;
|
|
15
15
|
export declare class RebaseApiError extends Error {
|
|
16
16
|
status: number;
|
|
17
17
|
code?: string;
|
|
18
|
-
details?:
|
|
19
|
-
constructor(status: number, message: string, code?: string, details?:
|
|
18
|
+
details?: unknown;
|
|
19
|
+
constructor(status: number, message: string, code?: string, details?: unknown);
|
|
20
20
|
}
|
|
21
21
|
export declare function buildQueryString(params?: FindParams): string;
|
|
22
22
|
export interface Transport {
|
|
23
|
-
request: <T =
|
|
23
|
+
request: <T = unknown>(path: string, init?: RequestInit) => Promise<T>;
|
|
24
24
|
setToken: (newToken: string | null) => void;
|
|
25
25
|
setAuthTokenGetter: (getter: () => Promise<string | null>) => void;
|
|
26
26
|
setOnUnauthorized: (handler: () => Promise<boolean>) => void;
|
package/dist/websocket.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DeleteEntityProps, Entity, EntityCollection, FetchCollectionProps, FetchEntityProps, SaveEntityProps, TableMetadata } from "@rebasepro/types";
|
|
1
|
+
import { DeleteEntityProps, Entity, EntityCollection, FetchCollectionProps, FetchEntityProps, SaveEntityProps, TableMetadata, BranchInfo } from "@rebasepro/types";
|
|
2
2
|
export interface RebaseWebSocketConfig {
|
|
3
3
|
websocketUrl: string;
|
|
4
4
|
/** Optional auth token getter for WebSocket authentication */
|
|
@@ -17,7 +17,7 @@ export declare class RebaseWebSocketClient {
|
|
|
17
17
|
getAuthToken?: () => Promise<string>;
|
|
18
18
|
private subscriptions;
|
|
19
19
|
private listeners;
|
|
20
|
-
on(event: "connect" | "disconnect" | "reconnect" | "error", cb:
|
|
20
|
+
on(event: "connect" | "disconnect" | "reconnect" | "error", cb: (...args: unknown[]) => void): () => boolean;
|
|
21
21
|
private emit;
|
|
22
22
|
private collectionSubscriptions;
|
|
23
23
|
private entitySubscriptions;
|
|
@@ -28,6 +28,7 @@ export declare class RebaseWebSocketClient {
|
|
|
28
28
|
private maxReconnectAttempts;
|
|
29
29
|
private isConnected;
|
|
30
30
|
private messageQueue;
|
|
31
|
+
private reconnectTimeout;
|
|
31
32
|
private isAuthenticated;
|
|
32
33
|
private authPromise;
|
|
33
34
|
private WebSocketConstructor;
|
|
@@ -52,10 +53,10 @@ export declare class RebaseWebSocketClient {
|
|
|
52
53
|
reauthenticate(): Promise<void>;
|
|
53
54
|
private sendMessage;
|
|
54
55
|
private doSendMessage;
|
|
55
|
-
fetchCollection<M extends Record<string,
|
|
56
|
-
fetchEntity<M extends Record<string,
|
|
57
|
-
saveEntity<M extends Record<string,
|
|
58
|
-
deleteEntity<M extends Record<string,
|
|
56
|
+
fetchCollection<M extends Record<string, unknown>>(props: FetchCollectionProps<M>): Promise<Entity<M>[]>;
|
|
57
|
+
fetchEntity<M extends Record<string, unknown>>(props: FetchEntityProps<M>): Promise<Entity<M> | undefined>;
|
|
58
|
+
saveEntity<M extends Record<string, unknown>>(props: SaveEntityProps<M>): Promise<Entity<M>>;
|
|
59
|
+
deleteEntity<M extends Record<string, unknown>>(props: DeleteEntityProps<M>): Promise<void>;
|
|
59
60
|
executeSql(sql: string, options?: {
|
|
60
61
|
database?: string;
|
|
61
62
|
role?: string;
|
|
@@ -63,10 +64,15 @@ export declare class RebaseWebSocketClient {
|
|
|
63
64
|
fetchAvailableDatabases(): Promise<string[]>;
|
|
64
65
|
fetchAvailableRoles(): Promise<string[]>;
|
|
65
66
|
fetchCurrentDatabase(): Promise<string | undefined>;
|
|
66
|
-
checkUniqueField(path: string, name: string, value:
|
|
67
|
-
countEntities<M extends Record<string,
|
|
67
|
+
checkUniqueField(path: string, name: string, value: unknown, entityId?: string, collection?: EntityCollection): Promise<boolean>;
|
|
68
|
+
countEntities<M extends Record<string, unknown>>(props: FetchCollectionProps<M>): Promise<number>;
|
|
68
69
|
fetchUnmappedTables(mappedPaths?: string[]): Promise<string[]>;
|
|
69
70
|
fetchTableMetadata(tableName: string): Promise<TableMetadata>;
|
|
71
|
+
createBranch(name: string, options?: {
|
|
72
|
+
source?: string;
|
|
73
|
+
}): Promise<BranchInfo>;
|
|
74
|
+
deleteBranch(name: string): Promise<void>;
|
|
75
|
+
listBranches(): Promise<BranchInfo[]>;
|
|
70
76
|
/**
|
|
71
77
|
* Recursively compare two values for structural equality.
|
|
72
78
|
* Handles primitives, null, undefined, Date, RegExp, arrays, and plain objects.
|
|
@@ -80,8 +86,8 @@ export declare class RebaseWebSocketClient {
|
|
|
80
86
|
* haven't actually changed.
|
|
81
87
|
*/
|
|
82
88
|
private mergeEntities;
|
|
83
|
-
listenCollection<M extends Record<string,
|
|
84
|
-
listenEntity<M extends Record<string,
|
|
89
|
+
listenCollection<M extends Record<string, unknown>>(props: FetchCollectionProps<M>, onUpdate: (entities: Entity[]) => void, onError?: (error: Error) => void): () => void;
|
|
90
|
+
listenEntity<M extends Record<string, unknown>>(props: FetchEntityProps<M>, onUpdate: (entity: Entity | null) => void, onError?: (error: Error) => void): () => void;
|
|
85
91
|
/**
|
|
86
92
|
* Re-send all active subscriptions to the backend after a reconnect.
|
|
87
93
|
* The server wipes subscription state when a client disconnects, so
|
package/package.json
CHANGED
|
@@ -1,79 +1,83 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
"name": "@rebasepro/client",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1-canary.ca2cb6e",
|
|
5
|
+
"description": "HTTP SDK client for the Rebase custom backend",
|
|
6
|
+
"funding": {
|
|
7
|
+
"url": "https://github.com/sponsors/rebaseco"
|
|
8
|
+
},
|
|
9
|
+
"author": "Rebase",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/rebasepro/rebase.git",
|
|
14
|
+
"directory": "packages/client"
|
|
15
|
+
},
|
|
16
|
+
"main": "./dist/index.umd.cjs",
|
|
17
|
+
"module": "./dist/index.es.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"source": "src/index.ts",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=14"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"development": "./src/index.ts",
|
|
27
|
+
"import": "./dist/index.es.js",
|
|
28
|
+
"require": "./dist/index.umd.cjs"
|
|
8
29
|
},
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@rebasepro/utils": "0.0.1-canary.ca2cb6e",
|
|
34
|
+
"@rebasepro/types": "0.0.1-canary.ca2cb6e"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@jest/globals": "^29.7.0",
|
|
38
|
+
"@types/jest": "^29.5.14",
|
|
39
|
+
"@types/node": "^20.17.14",
|
|
40
|
+
"cross-env": "^7.0.3",
|
|
41
|
+
"jest": "^29.7.0",
|
|
42
|
+
"npm-run-all": "^4.1.5",
|
|
43
|
+
"ts-jest": "^29.3.1",
|
|
44
|
+
"ts-node": "^10.9.2",
|
|
45
|
+
"tsd": "^0.31.2",
|
|
46
|
+
"typescript": "^5.8.3",
|
|
47
|
+
"vite": "^5.4.17"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"dist",
|
|
51
|
+
"src"
|
|
52
|
+
],
|
|
53
|
+
"gitHead": "d935eefa5aa8d1009a2398cfac2c1e4ee9aeb6b6",
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"jest": {
|
|
58
|
+
"transform": {
|
|
59
|
+
"^.+\\.tsx?$": "ts-jest"
|
|
15
60
|
},
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"watch": "vite build --watch",
|
|
25
|
-
"build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json",
|
|
26
|
-
"prepublishOnly": "pnpm run build",
|
|
27
|
-
"test:lint": "eslint \"src/**\" --quiet",
|
|
28
|
-
"test": "jest --passWithNoTests --forceExit",
|
|
29
|
-
"clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f"
|
|
30
|
-
},
|
|
31
|
-
"exports": {
|
|
32
|
-
".": {
|
|
33
|
-
"types": "./dist/index.d.ts",
|
|
34
|
-
"development": "./src/index.ts",
|
|
35
|
-
"import": "./dist/index.es.js",
|
|
36
|
-
"require": "./dist/index.umd.cjs"
|
|
37
|
-
},
|
|
38
|
-
"./package.json": "./package.json"
|
|
39
|
-
},
|
|
40
|
-
"dependencies": {
|
|
41
|
-
"@rebasepro/types": "0.0.1-canary.4d4fb3e"
|
|
42
|
-
},
|
|
43
|
-
"devDependencies": {
|
|
44
|
-
"@jest/globals": "^29.7.0",
|
|
45
|
-
"@types/jest": "^29.5.14",
|
|
46
|
-
"@types/node": "^20.17.14",
|
|
47
|
-
"cross-env": "^7.0.3",
|
|
48
|
-
"jest": "^29.7.0",
|
|
49
|
-
"npm-run-all": "^4.1.5",
|
|
50
|
-
"ts-jest": "^29.3.1",
|
|
51
|
-
"ts-node": "^10.9.2",
|
|
52
|
-
"tsd": "^0.31.2",
|
|
53
|
-
"typescript": "^5.8.3",
|
|
54
|
-
"vite": "^5.4.17"
|
|
55
|
-
},
|
|
56
|
-
"files": [
|
|
57
|
-
"dist",
|
|
58
|
-
"src"
|
|
61
|
+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
|
|
62
|
+
"moduleFileExtensions": [
|
|
63
|
+
"ts",
|
|
64
|
+
"tsx",
|
|
65
|
+
"js",
|
|
66
|
+
"jsx",
|
|
67
|
+
"json",
|
|
68
|
+
"node"
|
|
59
69
|
],
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"jest": {
|
|
65
|
-
"transform": {
|
|
66
|
-
"^.+\\.tsx?$": "ts-jest"
|
|
67
|
-
},
|
|
68
|
-
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
|
|
69
|
-
"moduleFileExtensions": [
|
|
70
|
-
"ts",
|
|
71
|
-
"tsx",
|
|
72
|
-
"js",
|
|
73
|
-
"jsx",
|
|
74
|
-
"json",
|
|
75
|
-
"node"
|
|
76
|
-
],
|
|
77
|
-
"testEnvironment": "node"
|
|
70
|
+
"testEnvironment": "node",
|
|
71
|
+
"moduleNameMapper": {
|
|
72
|
+
"^@rebasepro/types$": "<rootDir>/../types/src/index.ts",
|
|
73
|
+
"^@rebasepro/utils$": "<rootDir>/../utils/src/index.ts"
|
|
78
74
|
}
|
|
79
|
-
}
|
|
75
|
+
},
|
|
76
|
+
"scripts": {
|
|
77
|
+
"watch": "vite build --watch",
|
|
78
|
+
"build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json",
|
|
79
|
+
"test:lint": "eslint \"src/**\" --quiet",
|
|
80
|
+
"test": "jest --passWithNoTests --forceExit",
|
|
81
|
+
"clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f"
|
|
82
|
+
}
|
|
83
|
+
}
|
package/src/admin.ts
CHANGED
|
@@ -15,8 +15,8 @@ export interface RebaseRole {
|
|
|
15
15
|
id: string;
|
|
16
16
|
name: string;
|
|
17
17
|
isAdmin: boolean;
|
|
18
|
-
defaultPermissions: Record<string,
|
|
19
|
-
config: Record<string,
|
|
18
|
+
defaultPermissions: Record<string, unknown> | null;
|
|
19
|
+
config: Record<string, unknown> | null;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export interface CreateAdminOptions {
|
|
@@ -51,20 +51,20 @@ export function createAdmin(transport: Transport, options?: CreateAdminOptions)
|
|
|
51
51
|
async function createUser(data: { email: string, displayName?: string, password?: string, roles?: string[] }) {
|
|
52
52
|
return transport.request<{ user: AdminUser }>(adminPath + "/users", {
|
|
53
53
|
method: "POST",
|
|
54
|
-
body: JSON.stringify(data)
|
|
54
|
+
body: JSON.stringify(data)
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
async function updateUser(userId: string, data: { email?: string, displayName?: string, password?: string, roles?: string[] }) {
|
|
59
59
|
return transport.request<{ user: AdminUser }>(adminPath + "/users/" + encodeURIComponent(userId), {
|
|
60
60
|
method: "PUT",
|
|
61
|
-
body: JSON.stringify(data)
|
|
61
|
+
body: JSON.stringify(data)
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
async function deleteUser(userId: string) {
|
|
66
66
|
return transport.request<{ success: boolean }>(adminPath + "/users/" + encodeURIComponent(userId), {
|
|
67
|
-
method: "DELETE"
|
|
67
|
+
method: "DELETE"
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
|
|
@@ -76,29 +76,29 @@ export function createAdmin(transport: Transport, options?: CreateAdminOptions)
|
|
|
76
76
|
return transport.request<{ role: RebaseRole }>(adminPath + "/roles/" + encodeURIComponent(roleId), { method: "GET" });
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
async function createRole(data: { id: string, name: string, isAdmin?: boolean, defaultPermissions?:
|
|
79
|
+
async function createRole(data: { id: string, name: string, isAdmin?: boolean, defaultPermissions?: Record<string, unknown>, config?: Record<string, unknown> }) {
|
|
80
80
|
return transport.request<{ role: RebaseRole }>(adminPath + "/roles", {
|
|
81
81
|
method: "POST",
|
|
82
|
-
body: JSON.stringify(data)
|
|
82
|
+
body: JSON.stringify(data)
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
async function updateRole(roleId: string, data: { name?: string, isAdmin?: boolean, defaultPermissions?:
|
|
86
|
+
async function updateRole(roleId: string, data: { name?: string, isAdmin?: boolean, defaultPermissions?: Record<string, unknown>, config?: Record<string, unknown> }) {
|
|
87
87
|
return transport.request<{ role: RebaseRole }>(adminPath + "/roles/" + encodeURIComponent(roleId), {
|
|
88
88
|
method: "PUT",
|
|
89
|
-
body: JSON.stringify(data)
|
|
89
|
+
body: JSON.stringify(data)
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
async function deleteRole(roleId: string) {
|
|
94
94
|
return transport.request<{ success: boolean }>(adminPath + "/roles/" + encodeURIComponent(roleId), {
|
|
95
|
-
method: "DELETE"
|
|
95
|
+
method: "DELETE"
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
async function bootstrap() {
|
|
100
100
|
return transport.request<{ success: boolean; message: string; user: { uid: string; roles: string[] } }>(adminPath + "/bootstrap", {
|
|
101
|
-
method: "POST"
|
|
101
|
+
method: "POST"
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
104
|
|
|
@@ -114,6 +114,6 @@ export function createAdmin(transport: Transport, options?: CreateAdminOptions)
|
|
|
114
114
|
createRole,
|
|
115
115
|
updateRole,
|
|
116
116
|
deleteRole,
|
|
117
|
-
bootstrap
|
|
117
|
+
bootstrap
|
|
118
118
|
};
|
|
119
119
|
}
|
package/src/auth.ts
CHANGED
|
@@ -24,7 +24,7 @@ export interface RebaseSession {
|
|
|
24
24
|
user: RebaseUser;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export type AuthChangeEvent =
|
|
27
|
+
export type AuthChangeEvent = "SIGNED_IN" | "SIGNED_OUT" | "TOKEN_REFRESHED" | "USER_UPDATED";
|
|
28
28
|
|
|
29
29
|
export interface AuthConfig {
|
|
30
30
|
needsSetup: boolean;
|
|
@@ -44,7 +44,7 @@ export function createMemoryStorage(): AuthStorage {
|
|
|
44
44
|
return {
|
|
45
45
|
getItem(key) { return store[key] ?? null; },
|
|
46
46
|
setItem(key, value) { store[key] = value; },
|
|
47
|
-
removeItem(key) { delete store[key]; }
|
|
47
|
+
removeItem(key) { delete store[key]; }
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
|
|
@@ -93,7 +93,7 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
93
93
|
status,
|
|
94
94
|
body?.error?.message || body?.message || statusText,
|
|
95
95
|
body?.error?.code || body?.code,
|
|
96
|
-
body?.error?.details || body?.details
|
|
96
|
+
body?.error?.details || body?.details
|
|
97
97
|
);
|
|
98
98
|
}
|
|
99
99
|
|
|
@@ -149,7 +149,7 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
149
149
|
accessToken: data.tokens.accessToken,
|
|
150
150
|
refreshToken: data.tokens.refreshToken,
|
|
151
151
|
expiresAt: data.tokens.accessTokenExpiresAt,
|
|
152
|
-
user: data.user
|
|
152
|
+
user: data.user
|
|
153
153
|
};
|
|
154
154
|
currentSession = session;
|
|
155
155
|
saveSession(session);
|
|
@@ -164,27 +164,33 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
164
164
|
const res = await fetchFn(authUrl("/login"), {
|
|
165
165
|
method: "POST",
|
|
166
166
|
headers: { "Content-Type": "application/json" },
|
|
167
|
-
body: JSON.stringify({ email,
|
|
167
|
+
body: JSON.stringify({ email,
|
|
168
|
+
password })
|
|
168
169
|
});
|
|
169
170
|
const body = await res.json().catch(() => ({}));
|
|
170
171
|
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
171
172
|
const session = handleAuthResponse(body, "SIGNED_IN");
|
|
172
|
-
return { user: session.user,
|
|
173
|
+
return { user: session.user,
|
|
174
|
+
accessToken: session.accessToken,
|
|
175
|
+
refreshToken: session.refreshToken };
|
|
173
176
|
}
|
|
174
177
|
|
|
175
178
|
async function signUp(email: string, password: string, displayName?: string) {
|
|
176
179
|
const fetchFn = getFetch();
|
|
177
|
-
const payload: Record<string, string> = { email,
|
|
180
|
+
const payload: Record<string, string> = { email,
|
|
181
|
+
password };
|
|
178
182
|
if (displayName !== undefined) payload.displayName = displayName;
|
|
179
183
|
const res = await fetchFn(authUrl("/register"), {
|
|
180
184
|
method: "POST",
|
|
181
185
|
headers: { "Content-Type": "application/json" },
|
|
182
|
-
body: JSON.stringify(payload)
|
|
186
|
+
body: JSON.stringify(payload)
|
|
183
187
|
});
|
|
184
188
|
const body = await res.json().catch(() => ({}));
|
|
185
189
|
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
186
190
|
const session = handleAuthResponse(body, "SIGNED_IN");
|
|
187
|
-
return { user: session.user,
|
|
191
|
+
return { user: session.user,
|
|
192
|
+
accessToken: session.accessToken,
|
|
193
|
+
refreshToken: session.refreshToken };
|
|
188
194
|
}
|
|
189
195
|
|
|
190
196
|
async function signInWithGoogle(idToken: string) {
|
|
@@ -192,12 +198,103 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
192
198
|
const res = await fetchFn(authUrl("/google"), {
|
|
193
199
|
method: "POST",
|
|
194
200
|
headers: { "Content-Type": "application/json" },
|
|
195
|
-
body: JSON.stringify({ idToken })
|
|
201
|
+
body: JSON.stringify({ idToken })
|
|
196
202
|
});
|
|
197
203
|
const body = await res.json().catch(() => ({}));
|
|
198
204
|
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
199
205
|
const session = handleAuthResponse(body, "SIGNED_IN");
|
|
200
|
-
return { user: session.user,
|
|
206
|
+
return { user: session.user,
|
|
207
|
+
accessToken: session.accessToken,
|
|
208
|
+
refreshToken: session.refreshToken };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function signInWithLinkedin(code: string, redirectUri: string) {
|
|
212
|
+
const fetchFn = getFetch();
|
|
213
|
+
const res = await fetchFn(authUrl("/linkedin"), {
|
|
214
|
+
method: "POST",
|
|
215
|
+
headers: { "Content-Type": "application/json" },
|
|
216
|
+
body: JSON.stringify({ code,
|
|
217
|
+
redirectUri })
|
|
218
|
+
});
|
|
219
|
+
const body = await res.json().catch(() => ({}));
|
|
220
|
+
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
221
|
+
const session = handleAuthResponse(body, "SIGNED_IN");
|
|
222
|
+
return { user: session.user,
|
|
223
|
+
accessToken: session.accessToken,
|
|
224
|
+
refreshToken: session.refreshToken };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Generic OAuth sign-in. Posts the given payload to `/auth/{providerId}`.
|
|
229
|
+
* Use this for any provider registered on the backend.
|
|
230
|
+
*/
|
|
231
|
+
async function signInWithOAuth(providerId: string, payload: Record<string, unknown>) {
|
|
232
|
+
const fetchFn = getFetch();
|
|
233
|
+
const res = await fetchFn(authUrl(`/${providerId}`), {
|
|
234
|
+
method: "POST",
|
|
235
|
+
headers: { "Content-Type": "application/json" },
|
|
236
|
+
body: JSON.stringify(payload)
|
|
237
|
+
});
|
|
238
|
+
const body = await res.json().catch(() => ({}));
|
|
239
|
+
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
240
|
+
const session = handleAuthResponse(body, "SIGNED_IN");
|
|
241
|
+
return { user: session.user,
|
|
242
|
+
accessToken: session.accessToken,
|
|
243
|
+
refreshToken: session.refreshToken };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Convenience wrappers for all supported OAuth providers
|
|
247
|
+
|
|
248
|
+
async function signInWithGitHub(code: string, redirectUri: string) {
|
|
249
|
+
return signInWithOAuth("github", { code,
|
|
250
|
+
redirectUri });
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async function signInWithMicrosoft(code: string, redirectUri: string) {
|
|
254
|
+
return signInWithOAuth("microsoft", { code,
|
|
255
|
+
redirectUri });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async function signInWithApple(code: string, redirectUri: string, user?: { name?: { firstName?: string; lastName?: string }; email?: string }) {
|
|
259
|
+
return signInWithOAuth("apple", { code,
|
|
260
|
+
redirectUri,
|
|
261
|
+
user });
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async function signInWithFacebook(code: string, redirectUri: string) {
|
|
265
|
+
return signInWithOAuth("facebook", { code,
|
|
266
|
+
redirectUri });
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function signInWithTwitter(code: string, redirectUri: string, codeVerifier: string) {
|
|
270
|
+
return signInWithOAuth("twitter", { code,
|
|
271
|
+
redirectUri,
|
|
272
|
+
codeVerifier });
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function signInWithDiscord(code: string, redirectUri: string) {
|
|
276
|
+
return signInWithOAuth("discord", { code,
|
|
277
|
+
redirectUri });
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async function signInWithGitLab(code: string, redirectUri: string) {
|
|
281
|
+
return signInWithOAuth("gitlab", { code,
|
|
282
|
+
redirectUri });
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function signInWithBitbucket(code: string, redirectUri: string) {
|
|
286
|
+
return signInWithOAuth("bitbucket", { code,
|
|
287
|
+
redirectUri });
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function signInWithSlack(code: string, redirectUri: string) {
|
|
291
|
+
return signInWithOAuth("slack", { code,
|
|
292
|
+
redirectUri });
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async function signInWithSpotify(code: string, redirectUri: string) {
|
|
296
|
+
return signInWithOAuth("spotify", { code,
|
|
297
|
+
redirectUri });
|
|
201
298
|
}
|
|
202
299
|
|
|
203
300
|
async function signOut() {
|
|
@@ -207,7 +304,7 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
207
304
|
await fetchFn(authUrl("/logout"), {
|
|
208
305
|
method: "POST",
|
|
209
306
|
headers: { "Content-Type": "application/json" },
|
|
210
|
-
body: JSON.stringify({ refreshToken: currentSession.refreshToken })
|
|
307
|
+
body: JSON.stringify({ refreshToken: currentSession.refreshToken })
|
|
211
308
|
});
|
|
212
309
|
}
|
|
213
310
|
} catch (e) { /* ignore */ }
|
|
@@ -229,7 +326,7 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
229
326
|
const res = await fetchFn(authUrl("/refresh"), {
|
|
230
327
|
method: "POST",
|
|
231
328
|
headers: { "Content-Type": "application/json" },
|
|
232
|
-
body: JSON.stringify({ refreshToken: currentSession.refreshToken })
|
|
329
|
+
body: JSON.stringify({ refreshToken: currentSession.refreshToken })
|
|
233
330
|
});
|
|
234
331
|
const body = await res.json().catch(() => ({}));
|
|
235
332
|
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
@@ -237,7 +334,7 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
237
334
|
accessToken: body.tokens.accessToken,
|
|
238
335
|
refreshToken: body.tokens.refreshToken,
|
|
239
336
|
expiresAt: body.tokens.accessTokenExpiresAt,
|
|
240
|
-
user: currentSession.user
|
|
337
|
+
user: currentSession.user
|
|
241
338
|
};
|
|
242
339
|
currentSession = session;
|
|
243
340
|
saveSession(session);
|
|
@@ -255,10 +352,11 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
255
352
|
async function updateUser(updates: { displayName?: string, photoURL?: string }) {
|
|
256
353
|
const data = await transport.request<{ user: RebaseUser }>(authPath + "/me", {
|
|
257
354
|
method: "PATCH",
|
|
258
|
-
body: JSON.stringify(updates)
|
|
355
|
+
body: JSON.stringify(updates)
|
|
259
356
|
});
|
|
260
357
|
if (currentSession) {
|
|
261
|
-
currentSession = { ...currentSession,
|
|
358
|
+
currentSession = { ...currentSession,
|
|
359
|
+
user: data.user };
|
|
262
360
|
saveSession(currentSession);
|
|
263
361
|
emit("USER_UPDATED", currentSession);
|
|
264
362
|
}
|
|
@@ -270,7 +368,7 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
270
368
|
const res = await fetchFn(authUrl("/forgot-password"), {
|
|
271
369
|
method: "POST",
|
|
272
370
|
headers: { "Content-Type": "application/json" },
|
|
273
|
-
body: JSON.stringify({ email })
|
|
371
|
+
body: JSON.stringify({ email })
|
|
274
372
|
});
|
|
275
373
|
const body = await res.json().catch(() => ({}));
|
|
276
374
|
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
@@ -282,7 +380,8 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
282
380
|
const res = await fetchFn(authUrl("/reset-password"), {
|
|
283
381
|
method: "POST",
|
|
284
382
|
headers: { "Content-Type": "application/json" },
|
|
285
|
-
body: JSON.stringify({ token,
|
|
383
|
+
body: JSON.stringify({ token,
|
|
384
|
+
password })
|
|
286
385
|
});
|
|
287
386
|
const body = await res.json().catch(() => ({}));
|
|
288
387
|
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
@@ -292,13 +391,14 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
292
391
|
async function changePassword(oldPassword: string, newPassword: string) {
|
|
293
392
|
return transport.request<{ success: boolean; message: string; }>(authPath + "/change-password", {
|
|
294
393
|
method: "POST",
|
|
295
|
-
body: JSON.stringify({ oldPassword,
|
|
394
|
+
body: JSON.stringify({ oldPassword,
|
|
395
|
+
newPassword })
|
|
296
396
|
});
|
|
297
397
|
}
|
|
298
398
|
|
|
299
399
|
async function sendVerificationEmail() {
|
|
300
400
|
return transport.request<{ success: boolean; message: string; }>(authPath + "/send-verification", {
|
|
301
|
-
method: "POST"
|
|
401
|
+
method: "POST"
|
|
302
402
|
});
|
|
303
403
|
}
|
|
304
404
|
|
|
@@ -306,7 +406,7 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
306
406
|
const fetchFn = getFetch();
|
|
307
407
|
const res = await fetchFn(authUrl("/verify-email?token=" + encodeURIComponent(token)), {
|
|
308
408
|
method: "GET",
|
|
309
|
-
headers: { "Content-Type": "application/json" }
|
|
409
|
+
headers: { "Content-Type": "application/json" }
|
|
310
410
|
});
|
|
311
411
|
const body = await res.json().catch(() => ({}));
|
|
312
412
|
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
@@ -320,13 +420,13 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
320
420
|
|
|
321
421
|
async function revokeSession(sessionId: string) {
|
|
322
422
|
return transport.request<{ success: boolean }>(authPath + "/sessions/" + encodeURIComponent(sessionId), {
|
|
323
|
-
method: "DELETE"
|
|
423
|
+
method: "DELETE"
|
|
324
424
|
});
|
|
325
425
|
}
|
|
326
426
|
|
|
327
427
|
async function revokeAllSessions() {
|
|
328
428
|
const result = await transport.request<{ success: boolean }>(authPath + "/sessions", {
|
|
329
|
-
method: "DELETE"
|
|
429
|
+
method: "DELETE"
|
|
330
430
|
});
|
|
331
431
|
currentSession = null;
|
|
332
432
|
clearStoredSession();
|
|
@@ -343,7 +443,7 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
343
443
|
const fetchFn = getFetch();
|
|
344
444
|
const res = await fetchFn(authUrl("/config"), {
|
|
345
445
|
method: "GET",
|
|
346
|
-
headers: { "Content-Type": "application/json" }
|
|
446
|
+
headers: { "Content-Type": "application/json" }
|
|
347
447
|
});
|
|
348
448
|
const body = await res.json().catch(() => ({}));
|
|
349
449
|
if (!res.ok) throwApiError(res.status, body, res.statusText);
|
|
@@ -381,6 +481,18 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
381
481
|
signInWithEmail,
|
|
382
482
|
signUp,
|
|
383
483
|
signInWithGoogle,
|
|
484
|
+
signInWithLinkedin,
|
|
485
|
+
signInWithOAuth,
|
|
486
|
+
signInWithGitHub,
|
|
487
|
+
signInWithMicrosoft,
|
|
488
|
+
signInWithApple,
|
|
489
|
+
signInWithFacebook,
|
|
490
|
+
signInWithTwitter,
|
|
491
|
+
signInWithDiscord,
|
|
492
|
+
signInWithGitLab,
|
|
493
|
+
signInWithBitbucket,
|
|
494
|
+
signInWithSlack,
|
|
495
|
+
signInWithSpotify,
|
|
384
496
|
signOut,
|
|
385
497
|
refreshSession,
|
|
386
498
|
getUser,
|
|
@@ -395,6 +507,6 @@ export function createAuth(transport: Transport, options?: CreateAuthOptions) {
|
|
|
395
507
|
revokeAllSessions,
|
|
396
508
|
getAuthConfig,
|
|
397
509
|
getSession,
|
|
398
|
-
onAuthStateChange
|
|
510
|
+
onAuthStateChange
|
|
399
511
|
};
|
|
400
512
|
}
|