@aghents/identity 0.1.0
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/index.d.ts +166 -0
- package/dist/index.js +56 -0
- package/package.json +35 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { Result } from 'neverthrow';
|
|
2
|
+
import { SupabaseClient, SupportedStorage } from '@supabase/supabase-js';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
type Database = {
|
|
6
|
+
identity: {
|
|
7
|
+
Tables: {
|
|
8
|
+
profiles: {
|
|
9
|
+
Row: {
|
|
10
|
+
created_at: string;
|
|
11
|
+
display_name: string | null;
|
|
12
|
+
entitlement: string;
|
|
13
|
+
handle: string;
|
|
14
|
+
id: string;
|
|
15
|
+
kind: string;
|
|
16
|
+
};
|
|
17
|
+
Insert: {
|
|
18
|
+
created_at?: string;
|
|
19
|
+
display_name?: string | null;
|
|
20
|
+
entitlement?: string;
|
|
21
|
+
handle: string;
|
|
22
|
+
id: string;
|
|
23
|
+
kind?: string;
|
|
24
|
+
};
|
|
25
|
+
Update: {
|
|
26
|
+
created_at?: string;
|
|
27
|
+
display_name?: string | null;
|
|
28
|
+
entitlement?: string;
|
|
29
|
+
handle?: string;
|
|
30
|
+
id?: string;
|
|
31
|
+
kind?: string;
|
|
32
|
+
};
|
|
33
|
+
Relationships: [];
|
|
34
|
+
};
|
|
35
|
+
ratings: {
|
|
36
|
+
Row: {
|
|
37
|
+
dimension: string;
|
|
38
|
+
game: string;
|
|
39
|
+
profile_id: string;
|
|
40
|
+
updated_at: string;
|
|
41
|
+
value: number;
|
|
42
|
+
};
|
|
43
|
+
Insert: {
|
|
44
|
+
dimension: string;
|
|
45
|
+
game: string;
|
|
46
|
+
profile_id: string;
|
|
47
|
+
updated_at?: string;
|
|
48
|
+
value?: number;
|
|
49
|
+
};
|
|
50
|
+
Update: {
|
|
51
|
+
dimension?: string;
|
|
52
|
+
game?: string;
|
|
53
|
+
profile_id?: string;
|
|
54
|
+
updated_at?: string;
|
|
55
|
+
value?: number;
|
|
56
|
+
};
|
|
57
|
+
Relationships: [
|
|
58
|
+
{
|
|
59
|
+
foreignKeyName: "ratings_profile_id_fkey";
|
|
60
|
+
columns: ["profile_id"];
|
|
61
|
+
isOneToOne: false;
|
|
62
|
+
referencedRelation: "profiles";
|
|
63
|
+
referencedColumns: ["id"];
|
|
64
|
+
}
|
|
65
|
+
];
|
|
66
|
+
};
|
|
67
|
+
waitlist: {
|
|
68
|
+
Row: {
|
|
69
|
+
created_at: string;
|
|
70
|
+
email: string;
|
|
71
|
+
id: number;
|
|
72
|
+
note: string | null;
|
|
73
|
+
};
|
|
74
|
+
Insert: {
|
|
75
|
+
created_at?: string;
|
|
76
|
+
email: string;
|
|
77
|
+
id?: never;
|
|
78
|
+
note?: string | null;
|
|
79
|
+
};
|
|
80
|
+
Update: {
|
|
81
|
+
created_at?: string;
|
|
82
|
+
email?: string;
|
|
83
|
+
id?: never;
|
|
84
|
+
note?: string | null;
|
|
85
|
+
};
|
|
86
|
+
Relationships: [];
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
Views: {
|
|
90
|
+
[_ in never]: never;
|
|
91
|
+
};
|
|
92
|
+
Functions: {
|
|
93
|
+
claim_handle: {
|
|
94
|
+
Args: {
|
|
95
|
+
p_handle: string;
|
|
96
|
+
};
|
|
97
|
+
Returns: {
|
|
98
|
+
created_at: string;
|
|
99
|
+
display_name: string | null;
|
|
100
|
+
entitlement: string;
|
|
101
|
+
handle: string;
|
|
102
|
+
id: string;
|
|
103
|
+
kind: string;
|
|
104
|
+
};
|
|
105
|
+
SetofOptions: {
|
|
106
|
+
from: "*";
|
|
107
|
+
to: "profiles";
|
|
108
|
+
isOneToOne: true;
|
|
109
|
+
isSetofReturn: false;
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
join_waitlist: {
|
|
113
|
+
Args: {
|
|
114
|
+
p_email: string;
|
|
115
|
+
p_note?: string;
|
|
116
|
+
};
|
|
117
|
+
Returns: undefined;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
Enums: {
|
|
121
|
+
[_ in never]: never;
|
|
122
|
+
};
|
|
123
|
+
CompositeTypes: {
|
|
124
|
+
[_ in never]: never;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
declare const schema: z.ZodObject<{
|
|
130
|
+
SUPABASE_URL: z.ZodString;
|
|
131
|
+
SUPABASE_ANON_KEY: z.ZodString;
|
|
132
|
+
}, "strip", z.ZodTypeAny, {
|
|
133
|
+
SUPABASE_URL: string;
|
|
134
|
+
SUPABASE_ANON_KEY: string;
|
|
135
|
+
}, {
|
|
136
|
+
SUPABASE_URL: string;
|
|
137
|
+
SUPABASE_ANON_KEY: string;
|
|
138
|
+
}>;
|
|
139
|
+
type IdentityEnv = z.infer<typeof schema>;
|
|
140
|
+
declare function readEnv(raw: Record<string, string | undefined>): IdentityEnv;
|
|
141
|
+
|
|
142
|
+
type IdentityClient = SupabaseClient<Database>;
|
|
143
|
+
type IdentityClientOptions = {
|
|
144
|
+
persistSession?: boolean;
|
|
145
|
+
storage?: SupportedStorage;
|
|
146
|
+
};
|
|
147
|
+
declare function createIdentityClient(env: IdentityEnv, opts?: IdentityClientOptions): IdentityClient;
|
|
148
|
+
|
|
149
|
+
type Profile = {
|
|
150
|
+
id: string;
|
|
151
|
+
handle: string;
|
|
152
|
+
displayName: string | null;
|
|
153
|
+
kind: "human" | "persona";
|
|
154
|
+
entitlement: string;
|
|
155
|
+
};
|
|
156
|
+
type IdentityError = {
|
|
157
|
+
code: "auth" | "db" | "taken";
|
|
158
|
+
message: string;
|
|
159
|
+
};
|
|
160
|
+
declare function getOrCreateGuest(client: IdentityClient): Promise<Result<{
|
|
161
|
+
userId: string;
|
|
162
|
+
}, IdentityError>>;
|
|
163
|
+
declare function claimHandle(client: IdentityClient, handle: string): Promise<Result<Profile, IdentityError>>;
|
|
164
|
+
declare function joinWaitlist(client: IdentityClient, email: string, note?: string): Promise<Result<void, IdentityError>>;
|
|
165
|
+
|
|
166
|
+
export { type Database, type IdentityClient, type IdentityEnv, type IdentityError, type Profile, claimHandle, createIdentityClient, getOrCreateGuest, joinWaitlist, readEnv };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { err, ok } from "neverthrow";
|
|
3
|
+
|
|
4
|
+
// src/client.ts
|
|
5
|
+
import { createClient } from "@supabase/supabase-js";
|
|
6
|
+
function createIdentityClient(env, opts = {}) {
|
|
7
|
+
const persistSession = opts.persistSession ?? true;
|
|
8
|
+
if (persistSession && !opts.storage && typeof localStorage === "undefined") {
|
|
9
|
+
throw new Error(
|
|
10
|
+
"createIdentityClient: no localStorage here; pass { storage } (AsyncStorage on native) or { persistSession: false }"
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
return createClient(env.SUPABASE_URL, env.SUPABASE_ANON_KEY, {
|
|
14
|
+
auth: { persistSession, storage: opts.storage }
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/env.ts
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
var schema = z.object({ SUPABASE_URL: z.string().url(), SUPABASE_ANON_KEY: z.string().min(20) });
|
|
21
|
+
function readEnv(raw) {
|
|
22
|
+
return schema.parse(raw);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/index.ts
|
|
26
|
+
var toProfile = (r) => ({
|
|
27
|
+
id: r.id,
|
|
28
|
+
handle: r.handle,
|
|
29
|
+
displayName: r.display_name,
|
|
30
|
+
kind: r.kind,
|
|
31
|
+
entitlement: r.entitlement
|
|
32
|
+
});
|
|
33
|
+
async function getOrCreateGuest(client) {
|
|
34
|
+
const existing = await client.auth.getSession();
|
|
35
|
+
if (existing.data.session) return ok({ userId: existing.data.session.user.id });
|
|
36
|
+
const { data, error } = await client.auth.signInAnonymously();
|
|
37
|
+
if (error || !data.user) return err({ code: "auth", message: error?.message ?? "no user" });
|
|
38
|
+
return ok({ userId: data.user.id });
|
|
39
|
+
}
|
|
40
|
+
async function claimHandle(client, handle) {
|
|
41
|
+
const { data, error } = await client.schema("identity").rpc("claim_handle", { p_handle: handle });
|
|
42
|
+
if (error) return err({ code: error.code === "23505" ? "taken" : "db", message: error.message });
|
|
43
|
+
return ok(toProfile(data));
|
|
44
|
+
}
|
|
45
|
+
async function joinWaitlist(client, email, note) {
|
|
46
|
+
const { error } = await client.schema("identity").rpc("join_waitlist", { p_email: email, p_note: note ?? void 0 });
|
|
47
|
+
if (error) return err({ code: "db", message: error.message });
|
|
48
|
+
return ok(void 0);
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
claimHandle,
|
|
52
|
+
createIdentityClient,
|
|
53
|
+
getOrCreateGuest,
|
|
54
|
+
joinWaitlist,
|
|
55
|
+
readEnv
|
|
56
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aghents/identity",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@supabase/supabase-js": "^2.110.2",
|
|
22
|
+
"neverthrow": "^8.1.1",
|
|
23
|
+
"zod": "^3.24.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"jsdom": "^25.0.1",
|
|
27
|
+
"typescript": "^5.9.3",
|
|
28
|
+
"vitest": "^2.1.9"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
32
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
33
|
+
"test": "vitest run"
|
|
34
|
+
}
|
|
35
|
+
}
|