cantian-rest 0.0.82 → 0.0.83
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 +8 -10
- package/dist/apiKey.js +8 -25
- package/dist/apiKey.js.map +1 -1
- package/package.json +1 -1
package/dist/apiKey.d.ts
CHANGED
|
@@ -16,20 +16,18 @@ export type ApiKeyListItem = {
|
|
|
16
16
|
createdAt: string;
|
|
17
17
|
updatedAt: string;
|
|
18
18
|
};
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
export declare const verifyApiKey: (apiKey: string) => Promise<ApiKeyPrincipal | undefined>;
|
|
20
|
+
export declare const createApiKey: (options: CreateApiKeyOptions) => Promise<{
|
|
21
21
|
key: string;
|
|
22
22
|
name: string;
|
|
23
23
|
sub: string;
|
|
24
|
-
scopes
|
|
25
|
-
client_id
|
|
24
|
+
scopes: string[] | undefined;
|
|
25
|
+
client_id: string | undefined;
|
|
26
26
|
createdAt: string;
|
|
27
27
|
updatedAt: string;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export declare const createApiKey: (options: CreateApiKeyOptions) => Promise<ApiKeyDocument>;
|
|
28
|
+
id: string;
|
|
29
|
+
}>;
|
|
31
30
|
export declare const deleteApiKey: (apiKey: string) => Promise<boolean>;
|
|
32
|
-
export declare const updateApiKeyName: (
|
|
33
|
-
export declare const listApiKeys: () => Promise<ApiKeyListItem[]>;
|
|
31
|
+
export declare const updateApiKeyName: (id: string, name: string) => Promise<boolean>;
|
|
32
|
+
export declare const listApiKeys: (sub: string) => Promise<ApiKeyListItem[]>;
|
|
34
33
|
export declare const buildCollection: () => Promise<void>;
|
|
35
|
-
export {};
|
package/dist/apiKey.js
CHANGED
|
@@ -2,12 +2,8 @@ import { nanoid } from 'cantian-mongodb';
|
|
|
2
2
|
import { db } from './db.js';
|
|
3
3
|
const mongoCollection = db.collection('api-key');
|
|
4
4
|
export const verifyApiKey = async (apiKey) => {
|
|
5
|
-
const key = apiKey?.trim();
|
|
6
|
-
if (!key) {
|
|
7
|
-
return undefined;
|
|
8
|
-
}
|
|
9
5
|
const document = await mongoCollection.findOne({
|
|
10
|
-
key,
|
|
6
|
+
key: apiKey,
|
|
11
7
|
});
|
|
12
8
|
if (!document) {
|
|
13
9
|
return undefined;
|
|
@@ -19,7 +15,6 @@ export const verifyApiKey = async (apiKey) => {
|
|
|
19
15
|
};
|
|
20
16
|
};
|
|
21
17
|
export const createApiKey = async (options) => {
|
|
22
|
-
const _id = nanoid(16);
|
|
23
18
|
const key = `ct-${nanoid(24)}`;
|
|
24
19
|
const now = new Date().toISOString();
|
|
25
20
|
const name = options.name?.trim();
|
|
@@ -31,7 +26,6 @@ export const createApiKey = async (options) => {
|
|
|
31
26
|
throw new Error('sub is required.');
|
|
32
27
|
}
|
|
33
28
|
const doc = {
|
|
34
|
-
_id,
|
|
35
29
|
key,
|
|
36
30
|
name,
|
|
37
31
|
sub,
|
|
@@ -40,35 +34,24 @@ export const createApiKey = async (options) => {
|
|
|
40
34
|
createdAt: now,
|
|
41
35
|
updatedAt: now,
|
|
42
36
|
};
|
|
43
|
-
await mongoCollection.insertOne(doc);
|
|
44
|
-
return doc;
|
|
37
|
+
const result = await mongoCollection.insertOne(doc);
|
|
38
|
+
return { id: result.insertedId, ...doc };
|
|
45
39
|
};
|
|
46
40
|
export const deleteApiKey = async (apiKey) => {
|
|
47
|
-
const
|
|
48
|
-
if (!key) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
const result = await mongoCollection.deleteOne({ key });
|
|
41
|
+
const result = await mongoCollection.deleteOne({ key: apiKey });
|
|
52
42
|
return result.deletedCount > 0;
|
|
53
43
|
};
|
|
54
|
-
export const updateApiKeyName = async (
|
|
55
|
-
const id = apiKey?.trim();
|
|
56
|
-
const newName = name?.trim();
|
|
57
|
-
if (!id || !newName) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
44
|
+
export const updateApiKeyName = async (id, name) => {
|
|
60
45
|
const result = await mongoCollection.updateOne({ _id: id }, {
|
|
61
46
|
$set: {
|
|
62
|
-
name
|
|
47
|
+
name,
|
|
63
48
|
updatedAt: new Date().toISOString(),
|
|
64
49
|
},
|
|
65
50
|
});
|
|
66
51
|
return result.matchedCount > 0;
|
|
67
52
|
};
|
|
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();
|
|
53
|
+
export const listApiKeys = async (sub) => {
|
|
54
|
+
const docs = await mongoCollection.find({ sub }).toArray();
|
|
72
55
|
return docs.map(({ _id: id, ...rest }) => ({ id, ...rest }));
|
|
73
56
|
};
|
|
74
57
|
export const buildCollection = async () => {
|
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;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,
|
|
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,QAAQ,GAA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