@grest-ts/common 0.0.5
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/LICENSE +21 -0
- package/README.md +81 -0
- package/dist/src/GGAsyncStorage.d.ts +12 -0
- package/dist/src/GGAsyncStorage.d.ts.map +1 -0
- package/dist/src/GGAsyncStorage.js +20 -0
- package/dist/src/GGAsyncStorage.js.map +1 -0
- package/dist/src/GGError.d.ts +32 -0
- package/dist/src/GGError.d.ts.map +1 -0
- package/dist/src/GGError.js +47 -0
- package/dist/src/GGError.js.map +1 -0
- package/dist/src/GGExtensionDiscovery.d.ts +54 -0
- package/dist/src/GGExtensionDiscovery.d.ts.map +1 -0
- package/dist/src/GGExtensionDiscovery.js +281 -0
- package/dist/src/GGExtensionDiscovery.js.map +1 -0
- package/dist/src/Secret.d.ts +46 -0
- package/dist/src/Secret.d.ts.map +1 -0
- package/dist/src/Secret.js +68 -0
- package/dist/src/Secret.js.map +1 -0
- package/dist/src/UnreachableCode.d.ts +5 -0
- package/dist/src/UnreachableCode.d.ts.map +1 -0
- package/dist/src/UnreachableCode.js +9 -0
- package/dist/src/UnreachableCode.js.map +1 -0
- package/dist/src/deepClone.d.ts +6 -0
- package/dist/src/deepClone.d.ts.map +1 -0
- package/dist/src/deepClone.js +38 -0
- package/dist/src/deepClone.js.map +1 -0
- package/dist/src/deepFreeze.d.ts +6 -0
- package/dist/src/deepFreeze.d.ts.map +1 -0
- package/dist/src/deepFreeze.js +22 -0
- package/dist/src/deepFreeze.js.map +1 -0
- package/dist/src/environment.d.ts +14 -0
- package/dist/src/environment.d.ts.map +1 -0
- package/dist/src/environment.js +18 -0
- package/dist/src/environment.js.map +1 -0
- package/dist/src/http.d.ts +50 -0
- package/dist/src/http.d.ts.map +1 -0
- package/dist/src/http.js +50 -0
- package/dist/src/http.js.map +1 -0
- package/dist/src/index-browser.d.ts +12 -0
- package/dist/src/index-browser.d.ts.map +1 -0
- package/dist/src/index-browser.js +13 -0
- package/dist/src/index-browser.js.map +1 -0
- package/dist/src/index-node.d.ts +13 -0
- package/dist/src/index-node.d.ts.map +1 -0
- package/dist/src/index-node.js +13 -0
- package/dist/src/index-node.js.map +1 -0
- package/dist/src/sleep.d.ts +2 -0
- package/dist/src/sleep.d.ts.map +1 -0
- package/dist/src/sleep.js +4 -0
- package/dist/src/sleep.js.map +1 -0
- package/dist/src/tsconfig.json +17 -0
- package/dist/src/types.d.ts +15 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/withTimeout.d.ts +11 -0
- package/dist/src/withTimeout.d.ts.map +1 -0
- package/dist/src/withTimeout.js +22 -0
- package/dist/src/withTimeout.js.map +1 -0
- package/dist/tsconfig.publish.tsbuildinfo +1 -0
- package/package.json +58 -0
- package/src/GGAsyncStorage.ts +27 -0
- package/src/GGError.ts +74 -0
- package/src/GGExtensionDiscovery.ts +314 -0
- package/src/Secret.ts +76 -0
- package/src/UnreachableCode.ts +9 -0
- package/src/deepClone.ts +43 -0
- package/src/deepFreeze.ts +21 -0
- package/src/environment.ts +22 -0
- package/src/http.ts +52 -0
- package/src/index-browser.ts +12 -0
- package/src/index-node.ts +12 -0
- package/src/sleep.ts +3 -0
- package/src/tsconfig.json +17 -0
- package/src/types.ts +16 -0
- package/src/withTimeout.ts +20 -0
package/src/deepClone.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively clones an object and all its nested properties.
|
|
3
|
+
* Handles: primitives, arrays, plain objects, Date, Map, Set.
|
|
4
|
+
*/
|
|
5
|
+
export function deepClone<T>(obj: T): T {
|
|
6
|
+
if (obj === null || obj === undefined) {
|
|
7
|
+
return obj;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (typeof obj !== 'object') {
|
|
11
|
+
return obj;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (obj instanceof Date) {
|
|
15
|
+
return new Date(obj.getTime()) as T;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (obj instanceof Map) {
|
|
19
|
+
const clonedMap = new Map();
|
|
20
|
+
for (const [key, value] of obj) {
|
|
21
|
+
clonedMap.set(deepClone(key), deepClone(value));
|
|
22
|
+
}
|
|
23
|
+
return clonedMap as T;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (obj instanceof Set) {
|
|
27
|
+
const clonedSet = new Set();
|
|
28
|
+
for (const value of obj) {
|
|
29
|
+
clonedSet.add(deepClone(value));
|
|
30
|
+
}
|
|
31
|
+
return clonedSet as T;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (Array.isArray(obj)) {
|
|
35
|
+
return obj.map(item => deepClone(item)) as T;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const clonedObj: Record<string, unknown> = {};
|
|
39
|
+
for (const key of Object.keys(obj)) {
|
|
40
|
+
clonedObj[key] = deepClone((obj as Record<string, unknown>)[key]);
|
|
41
|
+
}
|
|
42
|
+
return clonedObj as T;
|
|
43
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively freezes an object and all its nested properties.
|
|
3
|
+
* Used to create immutable objects.
|
|
4
|
+
*/
|
|
5
|
+
export function deepFreeze<T>(o: T): T {
|
|
6
|
+
if (o === undefined) {
|
|
7
|
+
return o;
|
|
8
|
+
}
|
|
9
|
+
Object.freeze(o);
|
|
10
|
+
Object.getOwnPropertyNames(o).forEach(function (prop) {
|
|
11
|
+
if (prop === "renderers") {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if ((o as any)[prop] !== null
|
|
15
|
+
&& (typeof (o as any)[prop] === "object" || typeof (o as any)[prop] === "function")
|
|
16
|
+
&& !Object.isFrozen((o as any)[prop])) {
|
|
17
|
+
deepFreeze((o as any)[prop]);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return o;
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment detection utilities that work without DOM lib
|
|
3
|
+
* Uses type assertions to avoid TypeScript errors in Node-only contexts
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Declare window type to avoid TS errors when DOM lib is not included
|
|
7
|
+
declare const window: { document?: unknown } | undefined
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Check if code is running in a browser environment
|
|
11
|
+
* Works in both Node.js and browser contexts without requiring DOM lib
|
|
12
|
+
*/
|
|
13
|
+
export function isBrowser(): boolean {
|
|
14
|
+
return typeof window !== 'undefined' && typeof window.document !== 'undefined'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Check if code is running in a Node.js environment
|
|
19
|
+
*/
|
|
20
|
+
export function isNode(): boolean {
|
|
21
|
+
return !isBrowser()
|
|
22
|
+
}
|
package/src/http.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export enum HttpStatusCode {
|
|
2
|
+
OK200 = 200,
|
|
3
|
+
/**
|
|
4
|
+
* Completely fails to handle the request
|
|
5
|
+
*/
|
|
6
|
+
BadRequest400 = 400,
|
|
7
|
+
/**
|
|
8
|
+
* User must send auth headers or in other ways be "logged in".
|
|
9
|
+
*/
|
|
10
|
+
Unauthorized401 = 401,
|
|
11
|
+
/**
|
|
12
|
+
* User does not have access to resource
|
|
13
|
+
*/
|
|
14
|
+
Forbidden403 = 403,
|
|
15
|
+
/**
|
|
16
|
+
* Entity not found
|
|
17
|
+
*/
|
|
18
|
+
NotFound404 = 404,
|
|
19
|
+
/**
|
|
20
|
+
* Duplicate entity somewhere
|
|
21
|
+
*/
|
|
22
|
+
Exists409 = 409,
|
|
23
|
+
/**
|
|
24
|
+
* These are validation errors - automatic or manual.
|
|
25
|
+
*/
|
|
26
|
+
ValidationError422 = 422,
|
|
27
|
+
/**
|
|
28
|
+
* Method not allowed on this resource.
|
|
29
|
+
*/
|
|
30
|
+
MethodNotAllowed405 = 405,
|
|
31
|
+
/**
|
|
32
|
+
* Generic "something went wrong".
|
|
33
|
+
*/
|
|
34
|
+
InternalServerError500 = 500,
|
|
35
|
+
/**
|
|
36
|
+
* Bad Gateway - proxy/gateway received invalid response.
|
|
37
|
+
*/
|
|
38
|
+
BadGateway502 = 502,
|
|
39
|
+
/**
|
|
40
|
+
* Server is shutting down and is not able to handle the request.
|
|
41
|
+
*/
|
|
42
|
+
ServerTemporarilyNotAvailable503 = 503,
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Non standard HTTP error! This is used in the test framework in case request failes because of some testing check.
|
|
47
|
+
* Request itself maybe succeeded, but some check caused it to still fail. This never comes up in production, only in automated tests.
|
|
48
|
+
*/
|
|
49
|
+
TestingError = 800
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './deepFreeze';
|
|
2
|
+
export * from './deepClone';
|
|
3
|
+
export * from './withTimeout';
|
|
4
|
+
export * from './GGError';
|
|
5
|
+
export * from './UnreachableCode';
|
|
6
|
+
export * from './types';
|
|
7
|
+
export * from './http';
|
|
8
|
+
export * from './Secret';
|
|
9
|
+
export * from "./sleep"
|
|
10
|
+
export * from "./environment"
|
|
11
|
+
export * from "./GGAsyncStorage"
|
|
12
|
+
// GGExtensionDiscovery excluded — uses Node.js fs/path/url
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './deepFreeze';
|
|
2
|
+
export * from './deepClone';
|
|
3
|
+
export * from './withTimeout';
|
|
4
|
+
export * from './GGError';
|
|
5
|
+
export * from './UnreachableCode';
|
|
6
|
+
export * from './types';
|
|
7
|
+
export * from './http';
|
|
8
|
+
export * from './Secret';
|
|
9
|
+
export * from "./sleep"
|
|
10
|
+
export * from "./environment"
|
|
11
|
+
export * from "./GGAsyncStorage"
|
|
12
|
+
export * from "./GGExtensionDiscovery"
|
package/src/sleep.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively makes all properties of an object optional.
|
|
3
|
+
* Useful for partial matching in tests and validation.
|
|
4
|
+
*/
|
|
5
|
+
export type DeepPartial<T> = T extends object
|
|
6
|
+
? T extends Array<infer U>
|
|
7
|
+
? Array<DeepPartial<U>>
|
|
8
|
+
: { [P in keyof T]?: DeepPartial<T[P]> }
|
|
9
|
+
: T
|
|
10
|
+
|
|
11
|
+
export type ConstructorOf<T> = new (...args: any[]) => T;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns the instance of a class. T would be the class reference.
|
|
15
|
+
*/
|
|
16
|
+
export type InstanceOf<T> = T extends { new(...args: any[]): infer S } ? S : undefined
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a promise with a timeout. If the promise doesn't resolve/reject within
|
|
3
|
+
* the specified timeout, the returned promise will reject with an error.
|
|
4
|
+
*
|
|
5
|
+
* @param promise - The promise to wrap
|
|
6
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
7
|
+
* @param errorMessage - Error message to use when timeout occurs
|
|
8
|
+
* @returns A promise that resolves/rejects with the original promise or times out
|
|
9
|
+
*/
|
|
10
|
+
export async function withTimeout<T>(promise: Promise<T>, timeoutMs: number, errorMessage: string): Promise<T> {
|
|
11
|
+
let timeoutId: any;
|
|
12
|
+
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
13
|
+
timeoutId = setTimeout(() => reject(new Error(errorMessage)), timeoutMs);
|
|
14
|
+
});
|
|
15
|
+
try {
|
|
16
|
+
return await Promise.race([promise, timeoutPromise]);
|
|
17
|
+
} finally {
|
|
18
|
+
clearTimeout(timeoutId!);
|
|
19
|
+
}
|
|
20
|
+
}
|