cantian-rest 0.0.81 → 0.0.82
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/apiKey.d.ts +18 -0
- package/dist/apiKey.js +38 -0
- package/dist/apiKey.js.map +1 -1
- package/package.json +1 -1
package/dist/apiKey.d.ts
CHANGED
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
import { Auth } from './type.js';
|
|
2
2
|
export type ApiKeyPrincipal = Auth;
|
|
3
3
|
export type CreateApiKeyOptions = {
|
|
4
|
+
name: string;
|
|
4
5
|
sub: string;
|
|
5
6
|
scopes?: string[];
|
|
6
7
|
client_id?: string;
|
|
7
8
|
};
|
|
9
|
+
export type ApiKeyListItem = {
|
|
10
|
+
id: string;
|
|
11
|
+
key: string;
|
|
12
|
+
name: string;
|
|
13
|
+
sub: string;
|
|
14
|
+
scopes?: string[];
|
|
15
|
+
client_id?: string;
|
|
16
|
+
createdAt: string;
|
|
17
|
+
updatedAt: string;
|
|
18
|
+
};
|
|
8
19
|
type ApiKeyDocument = {
|
|
20
|
+
_id: string;
|
|
9
21
|
key: string;
|
|
22
|
+
name: string;
|
|
10
23
|
sub: string;
|
|
11
24
|
scopes?: string[];
|
|
12
25
|
client_id?: string;
|
|
26
|
+
createdAt: string;
|
|
27
|
+
updatedAt: string;
|
|
13
28
|
};
|
|
14
29
|
export declare const verifyApiKey: (apiKey: string) => Promise<ApiKeyPrincipal | undefined>;
|
|
15
30
|
export declare const createApiKey: (options: CreateApiKeyOptions) => Promise<ApiKeyDocument>;
|
|
31
|
+
export declare const deleteApiKey: (apiKey: string) => Promise<boolean>;
|
|
32
|
+
export declare const updateApiKeyName: (apiKey: string, name: string) => Promise<boolean>;
|
|
33
|
+
export declare const listApiKeys: () => Promise<ApiKeyListItem[]>;
|
|
16
34
|
export declare const buildCollection: () => Promise<void>;
|
|
17
35
|
export {};
|
package/dist/apiKey.js
CHANGED
|
@@ -19,20 +19,58 @@ export const verifyApiKey = async (apiKey) => {
|
|
|
19
19
|
};
|
|
20
20
|
};
|
|
21
21
|
export const createApiKey = async (options) => {
|
|
22
|
+
const _id = nanoid(16);
|
|
22
23
|
const key = `ct-${nanoid(24)}`;
|
|
24
|
+
const now = new Date().toISOString();
|
|
25
|
+
const name = options.name?.trim();
|
|
23
26
|
const sub = options.sub?.trim();
|
|
27
|
+
if (!name) {
|
|
28
|
+
throw new Error('name is required.');
|
|
29
|
+
}
|
|
24
30
|
if (!sub) {
|
|
25
31
|
throw new Error('sub is required.');
|
|
26
32
|
}
|
|
27
33
|
const doc = {
|
|
34
|
+
_id,
|
|
28
35
|
key,
|
|
36
|
+
name,
|
|
29
37
|
sub,
|
|
30
38
|
scopes: options.scopes,
|
|
31
39
|
client_id: options.client_id?.trim(),
|
|
40
|
+
createdAt: now,
|
|
41
|
+
updatedAt: now,
|
|
32
42
|
};
|
|
33
43
|
await mongoCollection.insertOne(doc);
|
|
34
44
|
return doc;
|
|
35
45
|
};
|
|
46
|
+
export const deleteApiKey = async (apiKey) => {
|
|
47
|
+
const key = apiKey?.trim();
|
|
48
|
+
if (!key) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const result = await mongoCollection.deleteOne({ key });
|
|
52
|
+
return result.deletedCount > 0;
|
|
53
|
+
};
|
|
54
|
+
export const updateApiKeyName = async (apiKey, name) => {
|
|
55
|
+
const id = apiKey?.trim();
|
|
56
|
+
const newName = name?.trim();
|
|
57
|
+
if (!id || !newName) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const result = await mongoCollection.updateOne({ _id: id }, {
|
|
61
|
+
$set: {
|
|
62
|
+
name: newName,
|
|
63
|
+
updatedAt: new Date().toISOString(),
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
return result.matchedCount > 0;
|
|
67
|
+
};
|
|
68
|
+
export const listApiKeys = async () => {
|
|
69
|
+
const docs = await mongoCollection
|
|
70
|
+
.find({}, { projection: { _id: 1, key: 1, name: 1, sub: 1, scopes: 1, client_id: 1, createdAt: 1, updatedAt: 1 } })
|
|
71
|
+
.toArray();
|
|
72
|
+
return docs.map(({ _id: id, ...rest }) => ({ id, ...rest }));
|
|
73
|
+
};
|
|
36
74
|
export const buildCollection = async () => {
|
|
37
75
|
await mongoCollection.createIndex({ key: 1 }, { unique: true });
|
|
38
76
|
};
|
package/dist/apiKey.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiKey.js","sourceRoot":"","sources":["../src/apiKey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"apiKey.js","sourceRoot":"","sources":["../src/apiKey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAgC7B,MAAM,eAAe,GAA+B,EAAE,CAAC,UAAU,CAAiB,SAAS,CAAC,CAAC;AAE7F,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAAE,MAAc,EAAwC,EAAE;IACzF,MAAM,GAAG,GAAG,MAAM,EAAE,IAAI,EAAE,CAAC;IAC3B,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,eAAe,CAAC,OAAO,CAAC;QAC7C,GAAG;KACJ,CAAC,CAAC;IACH,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO;QACL,GAAG,EAAE,QAAQ,CAAC,GAAG;QACjB,MAAM,EAAE,QAAQ,CAAC,MAAM;QACvB,SAAS,EAAE,QAAQ,CAAC,SAAS;KAC9B,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAA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