@aptly-as/sdk-nodejs 0.1.2 → 0.2.1
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/error/ResponseError.js +1 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/mongoose/mongoose.d.ts +13 -0
- package/mongoose/mongoose.js +45 -0
- package/package.json +5 -3
- package/tsconfig.prod.json +8 -0
package/error/ResponseError.js
CHANGED
package/index.d.ts
CHANGED
package/index.js
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
import mongoose from 'mongoose';
|
2
|
+
type MongooseId = mongoose.Types.ObjectId | string;
|
3
|
+
type MongooseDocument = mongoose.Document<mongoose.Types.ObjectId>;
|
4
|
+
type MongooseIdOrDocument = MongooseId | MongooseDocument;
|
5
|
+
export declare function getStringId<T extends string | {
|
6
|
+
_id: string;
|
7
|
+
} | null | undefined>(idOrDocument: T): string;
|
8
|
+
export declare function getObjectId(IdOrDocument?: MongooseIdOrDocument): mongoose.Types.ObjectId | null | undefined;
|
9
|
+
export declare function toObjectId(_id: MongooseId): mongoose.Types.ObjectId;
|
10
|
+
export declare function isId(_id?: MongooseId): boolean | RegExpMatchArray | null;
|
11
|
+
export declare function matchObjectId(a?: MongooseIdOrDocument, b?: MongooseIdOrDocument): boolean;
|
12
|
+
export declare function byObjectId(id?: MongooseIdOrDocument): (obj?: MongooseIdOrDocument) => boolean;
|
13
|
+
export {};
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import mongoose from 'mongoose';
|
2
|
+
export function getStringId(idOrDocument) {
|
3
|
+
if (!idOrDocument)
|
4
|
+
return '';
|
5
|
+
if (typeof idOrDocument === 'string')
|
6
|
+
return idOrDocument;
|
7
|
+
return String(idOrDocument._id);
|
8
|
+
}
|
9
|
+
export function getObjectId(IdOrDocument) {
|
10
|
+
if (!IdOrDocument)
|
11
|
+
return null;
|
12
|
+
if (IdOrDocument instanceof mongoose.Document) {
|
13
|
+
return IdOrDocument._id;
|
14
|
+
}
|
15
|
+
if (typeof IdOrDocument === 'string')
|
16
|
+
return new mongoose.Types.ObjectId(IdOrDocument);
|
17
|
+
if ('_id' in IdOrDocument) {
|
18
|
+
if (typeof IdOrDocument._id === 'string')
|
19
|
+
return new mongoose.Types.ObjectId(IdOrDocument._id);
|
20
|
+
return IdOrDocument._id;
|
21
|
+
}
|
22
|
+
return null;
|
23
|
+
}
|
24
|
+
export function toObjectId(_id) {
|
25
|
+
if (typeof _id === 'string')
|
26
|
+
return new mongoose.Types.ObjectId(_id);
|
27
|
+
return _id;
|
28
|
+
}
|
29
|
+
export function isId(_id) {
|
30
|
+
if (_id instanceof mongoose.Types.ObjectId)
|
31
|
+
return true;
|
32
|
+
return typeof _id === 'string' && _id.match(/^[a-fA-F0-9]{24}$/);
|
33
|
+
}
|
34
|
+
export function matchObjectId(a, b) {
|
35
|
+
if (!a || !b)
|
36
|
+
return a === b;
|
37
|
+
const aa = getObjectId(a);
|
38
|
+
const bb = getObjectId(b);
|
39
|
+
if (!aa || !bb)
|
40
|
+
return aa === bb;
|
41
|
+
return aa.equals(bb);
|
42
|
+
}
|
43
|
+
export function byObjectId(id) {
|
44
|
+
return (obj) => matchObjectId(obj, id);
|
45
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@aptly-as/sdk-nodejs",
|
3
|
-
"version": "0.1
|
3
|
+
"version": "0.2.1",
|
4
4
|
"description": "Aptly SDK library for node.js applications",
|
5
5
|
"type": "module",
|
6
6
|
"main": "index.js",
|
@@ -11,19 +11,21 @@
|
|
11
11
|
"@vitest/ui": "^2.1.8",
|
12
12
|
"husky": "^9.1.7",
|
13
13
|
"lint-staged": "^15.2.11",
|
14
|
+
"mongoose": "^8.9.5",
|
14
15
|
"prettier": "^3.4.2",
|
15
16
|
"typescript": "^5.7.2",
|
16
17
|
"vitest": "^2.1.8"
|
17
18
|
},
|
18
19
|
"peerDependencies": {
|
19
|
-
"@aptly-as/types": "*"
|
20
|
+
"@aptly-as/types": "*",
|
21
|
+
"mongoose": "*"
|
20
22
|
},
|
21
23
|
"lint-staged": {
|
22
24
|
"**/*": "prettier --write --ignore-unknown"
|
23
25
|
},
|
24
26
|
"scripts": {
|
25
27
|
"start": "tsc --incremental --watch",
|
26
|
-
"build": "tsc",
|
28
|
+
"build": "tsc --project tsconfig.prod.json",
|
27
29
|
"test": "vitest",
|
28
30
|
"test:ui": "vitest --ui --api 9527",
|
29
31
|
"test:run": "vitest run",
|