@ciderjs/gasnuki 0.4.1 → 0.5.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/README.ja.md +30 -3
- package/README.md +30 -3
- package/dist/cli.cjs +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/json.cjs +21 -0
- package/dist/json.d.cts +29 -0
- package/dist/json.d.mts +29 -0
- package/dist/json.d.ts +29 -0
- package/dist/json.mjs +18 -0
- package/dist/promise.cjs +34 -4
- package/dist/promise.d.cts +14 -2
- package/dist/promise.d.mts +14 -2
- package/dist/promise.d.ts +14 -2
- package/dist/promise.mjs +34 -4
- package/package.json +10 -5
package/README.ja.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# @ciderjs/gasnuki
|
|
2
2
|
|
|
3
3
|
[](./README.md)
|
|
4
|
-
[](https://github.com/luthpg/gasnuki)
|
|
5
5
|
[](LICENSE)
|
|
6
6
|
[](https://www.npmjs.com/package/@ciderjs/gasnuki)
|
|
7
|
+

|
|
7
8
|
[](https://github.com/luthpg/gasnuki/issues)
|
|
8
9
|
|
|
9
10
|
Google Apps Script クライアントサイドAPIの型定義・ユーティリティ
|
|
@@ -160,7 +161,7 @@ import {
|
|
|
160
161
|
import type { ServerScripts } from '../types/appsscript';
|
|
161
162
|
|
|
162
163
|
// 開発用のモック関数を定義します
|
|
163
|
-
const
|
|
164
|
+
const mockupFunctions: PartialScriptType<ServerScripts> = {
|
|
164
165
|
// sayHello関数の動作をシミュレート
|
|
165
166
|
sayHello: async (name) => {
|
|
166
167
|
await new Promise(resolve => setTimeout(resolve, 500)); // ネットワーク遅延を模倣
|
|
@@ -169,7 +170,33 @@ const mockup: PartialScriptType<ServerScripts> = {
|
|
|
169
170
|
// 他の関数も同様にモックできます
|
|
170
171
|
};
|
|
171
172
|
|
|
172
|
-
export const gas = getPromisedServerScripts<ServerScripts>(
|
|
173
|
+
export const gas = getPromisedServerScripts<ServerScripts>({ mockupFunctions });
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### 型安全な JSON パース (Optional)
|
|
177
|
+
|
|
178
|
+
通常、Google Apps Script とクライアント間の通信で `JSON.parse()` を使うと戻り値が `any` になってしまいます。また、`Date` 型などはシリアライズの過程で文字列に変換され、手動での復元が必要になります。
|
|
179
|
+
|
|
180
|
+
`gasnuki` は、シリアライズ前の型情報を Branded Type (`JsonString<T>`) として保持することで、**`any` を介さない型安全な復元**を可能にします。
|
|
181
|
+
|
|
182
|
+
`getPromisedServerScripts` のオプションに `{ parseJson: true }` を指定すると、サーバー側で `serialize()` された戻り値を自動でデシリアライズし、`Date` オブジェクトも正しく復元します。
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
// サーバー側 (Apps Script)
|
|
186
|
+
// const getAppData = () => serialize({ updatedAt: new Date(), user: 'Alice' });
|
|
187
|
+
|
|
188
|
+
// クライアント側
|
|
189
|
+
export const gas = getPromisedServerScripts<ServerScripts>({
|
|
190
|
+
parseJson: true
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
async function fetchData() {
|
|
194
|
+
// 戻り値は `any` ではなく、元のオブジェクト型として推論されます
|
|
195
|
+
// Date オブジェクトも自動的に復元されます
|
|
196
|
+
const result = await gas.getAppData();
|
|
197
|
+
console.log(result.user); // 'Alice' (string)
|
|
198
|
+
console.log(result.updatedAt instanceof Date); // true
|
|
199
|
+
}
|
|
173
200
|
```
|
|
174
201
|
|
|
175
202
|
## コントリビュート
|
package/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# @ciderjs/gasnuki
|
|
2
2
|
|
|
3
3
|
[](./README.ja.md)
|
|
4
|
-
[](https://github.com/luthpg/gasnuki)
|
|
5
5
|
[](LICENSE)
|
|
6
6
|
[](https://www.npmjs.com/package/@ciderjs/gasnuki)
|
|
7
|
+

|
|
7
8
|
[](https://github.com/luthpg/gasnuki/issues)
|
|
8
9
|
|
|
9
10
|
Type definitions and utilities for Google Apps Script client-side API
|
|
@@ -160,7 +161,7 @@ import {
|
|
|
160
161
|
import type { ServerScripts } from '../types/appsscript';
|
|
161
162
|
|
|
162
163
|
// Define mockup functions for development
|
|
163
|
-
const
|
|
164
|
+
const mockupFunctions: PartialScriptType<ServerScripts> = {
|
|
164
165
|
// Simulate the behavior of the sayHello function
|
|
165
166
|
sayHello: async (name) => {
|
|
166
167
|
await new Promise(resolve => setTimeout(resolve, 500)); // Simulate network delay
|
|
@@ -169,7 +170,33 @@ const mockup: PartialScriptType<ServerScripts> = {
|
|
|
169
170
|
// Other functions can be mocked similarly
|
|
170
171
|
};
|
|
171
172
|
|
|
172
|
-
export const gas = getPromisedServerScripts<ServerScripts>(
|
|
173
|
+
export const gas = getPromisedServerScripts<ServerScripts>({ mockupFunctions });
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Type-Safe JSON Parsing (Optional)
|
|
177
|
+
|
|
178
|
+
Normally, using `JSON.parse()` for communication between Google Apps Script and the client results in an `any` return type. Additionally, types like `Date` are converted to strings during serialization and require manual restoration.
|
|
179
|
+
|
|
180
|
+
`gasnuki` preserves the original type information as a Branded Type (`JsonString<T>`), enabling **type-safe restoration without using `any`**.
|
|
181
|
+
|
|
182
|
+
By passing `{ parseJson: true }` as the second argument to `getPromisedServerScripts`, it will automatically deserialize return values that were `serialize()`-ed on the server side, including proper `Date` object recovery.
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
// Server-side (Apps Script)
|
|
186
|
+
// const getAppData = () => serialize({ updatedAt: new Date(), user: 'Alice' });
|
|
187
|
+
|
|
188
|
+
// Client-side
|
|
189
|
+
export const gas = getPromisedServerScripts<ServerScripts>({
|
|
190
|
+
parseJson: true
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
async function fetchData() {
|
|
194
|
+
// The return value is inferred as the original object type instead of `any`
|
|
195
|
+
// Date objects are also automatically recovered
|
|
196
|
+
const result = await gas.getAppData();
|
|
197
|
+
console.log(result.user); // 'Alice' (string)
|
|
198
|
+
console.log(result.updatedAt instanceof Date); // true
|
|
199
|
+
}
|
|
173
200
|
```
|
|
174
201
|
|
|
175
202
|
## Contributing
|
package/dist/cli.cjs
CHANGED
package/dist/cli.mjs
CHANGED
package/dist/json.cjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/;
|
|
4
|
+
function dateReviver(_key, value) {
|
|
5
|
+
if (typeof value === "string" && isoDateRegex.test(value)) {
|
|
6
|
+
const date = new Date(value);
|
|
7
|
+
if (!Number.isNaN(date.getTime())) {
|
|
8
|
+
return date;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
const serialize = (data) => {
|
|
14
|
+
return JSON.stringify(data);
|
|
15
|
+
};
|
|
16
|
+
const deserialize = (json) => {
|
|
17
|
+
return JSON.parse(json, dateReviver);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
exports.deserialize = deserialize;
|
|
21
|
+
exports.serialize = serialize;
|
package/dist/json.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare const __brand: unique symbol;
|
|
2
|
+
/**
|
|
3
|
+
* シリアライズ前の型情報 `T` を保持したJSON文字列型。
|
|
4
|
+
* 単なる string ではなく、型システム上で元の型を記憶することで、
|
|
5
|
+
* 復元時に型安全なパース(anyを介さない復元)を可能にします。
|
|
6
|
+
*/
|
|
7
|
+
type JsonString<T> = string & {
|
|
8
|
+
[__brand]: T;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* オブジェクトを型情報を保持したまま JSON 文字列に変換します。
|
|
12
|
+
* 戻り値は元の型 `T` を記憶した `JsonString<T>` となります。
|
|
13
|
+
*
|
|
14
|
+
* @param data 変換するオブジェクト
|
|
15
|
+
* @returns 型情報を保持した JSON 文字列
|
|
16
|
+
*/
|
|
17
|
+
declare const serialize: <T>(data: T) => JsonString<T>;
|
|
18
|
+
/**
|
|
19
|
+
* `JsonString<T>` から元の型 `T` を型安全に復元します。
|
|
20
|
+
* 通常の `JSON.parse` と異なり、戻り値が `any` にならず、
|
|
21
|
+
* また ISO 8601 形式の文字列は自動的に `Date` オブジェクトへ変換されます。
|
|
22
|
+
*
|
|
23
|
+
* @param json 型情報を保持した JSON 文字列
|
|
24
|
+
* @returns 元の型 `T` に復元されたオブジェクト
|
|
25
|
+
*/
|
|
26
|
+
declare const deserialize: <T>(json: JsonString<T>) => T;
|
|
27
|
+
|
|
28
|
+
export { deserialize, serialize };
|
|
29
|
+
export type { JsonString };
|
package/dist/json.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare const __brand: unique symbol;
|
|
2
|
+
/**
|
|
3
|
+
* シリアライズ前の型情報 `T` を保持したJSON文字列型。
|
|
4
|
+
* 単なる string ではなく、型システム上で元の型を記憶することで、
|
|
5
|
+
* 復元時に型安全なパース(anyを介さない復元)を可能にします。
|
|
6
|
+
*/
|
|
7
|
+
type JsonString<T> = string & {
|
|
8
|
+
[__brand]: T;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* オブジェクトを型情報を保持したまま JSON 文字列に変換します。
|
|
12
|
+
* 戻り値は元の型 `T` を記憶した `JsonString<T>` となります。
|
|
13
|
+
*
|
|
14
|
+
* @param data 変換するオブジェクト
|
|
15
|
+
* @returns 型情報を保持した JSON 文字列
|
|
16
|
+
*/
|
|
17
|
+
declare const serialize: <T>(data: T) => JsonString<T>;
|
|
18
|
+
/**
|
|
19
|
+
* `JsonString<T>` から元の型 `T` を型安全に復元します。
|
|
20
|
+
* 通常の `JSON.parse` と異なり、戻り値が `any` にならず、
|
|
21
|
+
* また ISO 8601 形式の文字列は自動的に `Date` オブジェクトへ変換されます。
|
|
22
|
+
*
|
|
23
|
+
* @param json 型情報を保持した JSON 文字列
|
|
24
|
+
* @returns 元の型 `T` に復元されたオブジェクト
|
|
25
|
+
*/
|
|
26
|
+
declare const deserialize: <T>(json: JsonString<T>) => T;
|
|
27
|
+
|
|
28
|
+
export { deserialize, serialize };
|
|
29
|
+
export type { JsonString };
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare const __brand: unique symbol;
|
|
2
|
+
/**
|
|
3
|
+
* シリアライズ前の型情報 `T` を保持したJSON文字列型。
|
|
4
|
+
* 単なる string ではなく、型システム上で元の型を記憶することで、
|
|
5
|
+
* 復元時に型安全なパース(anyを介さない復元)を可能にします。
|
|
6
|
+
*/
|
|
7
|
+
type JsonString<T> = string & {
|
|
8
|
+
[__brand]: T;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* オブジェクトを型情報を保持したまま JSON 文字列に変換します。
|
|
12
|
+
* 戻り値は元の型 `T` を記憶した `JsonString<T>` となります。
|
|
13
|
+
*
|
|
14
|
+
* @param data 変換するオブジェクト
|
|
15
|
+
* @returns 型情報を保持した JSON 文字列
|
|
16
|
+
*/
|
|
17
|
+
declare const serialize: <T>(data: T) => JsonString<T>;
|
|
18
|
+
/**
|
|
19
|
+
* `JsonString<T>` から元の型 `T` を型安全に復元します。
|
|
20
|
+
* 通常の `JSON.parse` と異なり、戻り値が `any` にならず、
|
|
21
|
+
* また ISO 8601 形式の文字列は自動的に `Date` オブジェクトへ変換されます。
|
|
22
|
+
*
|
|
23
|
+
* @param json 型情報を保持した JSON 文字列
|
|
24
|
+
* @returns 元の型 `T` に復元されたオブジェクト
|
|
25
|
+
*/
|
|
26
|
+
declare const deserialize: <T>(json: JsonString<T>) => T;
|
|
27
|
+
|
|
28
|
+
export { deserialize, serialize };
|
|
29
|
+
export type { JsonString };
|
package/dist/json.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/;
|
|
2
|
+
function dateReviver(_key, value) {
|
|
3
|
+
if (typeof value === "string" && isoDateRegex.test(value)) {
|
|
4
|
+
const date = new Date(value);
|
|
5
|
+
if (!Number.isNaN(date.getTime())) {
|
|
6
|
+
return date;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
const serialize = (data) => {
|
|
12
|
+
return JSON.stringify(data);
|
|
13
|
+
};
|
|
14
|
+
const deserialize = (json) => {
|
|
15
|
+
return JSON.parse(json, dateReviver);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { deserialize, serialize };
|
package/dist/promise.cjs
CHANGED
|
@@ -1,19 +1,49 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const json = require('./json.cjs');
|
|
4
|
+
|
|
5
|
+
function getPromisedServerScripts(options = {}) {
|
|
6
|
+
const { mockupFunctions = {}, parseJson = false } = options;
|
|
4
7
|
return new Proxy(mockupFunctions, {
|
|
5
8
|
get(target, method) {
|
|
6
9
|
if (!("google" in globalThis) || !google?.script?.run) {
|
|
7
|
-
|
|
10
|
+
const mockFunc = target[method];
|
|
11
|
+
if (!mockFunc) {
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
14
|
+
if (!parseJson) {
|
|
15
|
+
return mockFunc;
|
|
16
|
+
}
|
|
17
|
+
return async (...args) => {
|
|
18
|
+
const res = await mockFunc(...args);
|
|
19
|
+
if (typeof res === "string") {
|
|
20
|
+
try {
|
|
21
|
+
return json.deserialize(res);
|
|
22
|
+
} catch {
|
|
23
|
+
return res;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return res;
|
|
27
|
+
};
|
|
8
28
|
}
|
|
9
29
|
if (!(method in google.script.run)) {
|
|
10
30
|
throw Error(`Method ${method} not found in AppsScript.`);
|
|
11
31
|
}
|
|
12
32
|
return (...args) => new Promise((resolve, reject) => {
|
|
13
|
-
google.script.run.withSuccessHandler(
|
|
33
|
+
google.script.run.withSuccessHandler((res) => {
|
|
34
|
+
if (parseJson && typeof res === "string") {
|
|
35
|
+
try {
|
|
36
|
+
resolve(json.deserialize(res));
|
|
37
|
+
} catch {
|
|
38
|
+
resolve(res);
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
resolve(res);
|
|
42
|
+
}
|
|
43
|
+
}).withFailureHandler(reject)[method](...args);
|
|
14
44
|
});
|
|
15
45
|
}
|
|
16
46
|
});
|
|
17
|
-
}
|
|
47
|
+
}
|
|
18
48
|
|
|
19
49
|
exports.getPromisedServerScripts = getPromisedServerScripts;
|
package/dist/promise.d.cts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
import { JsonString } from './json.cjs';
|
|
2
|
+
|
|
1
3
|
type Promised<T> = {
|
|
2
4
|
[K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<R> : T[K];
|
|
3
5
|
};
|
|
6
|
+
type UnwrapJson<T> = T extends JsonString<infer U> ? U : T;
|
|
7
|
+
type PromisedWithJson<T> = {
|
|
8
|
+
[K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<UnwrapJson<R>> : T[K];
|
|
9
|
+
};
|
|
4
10
|
type PartialScriptType<T> = Partial<Promised<T>>;
|
|
5
|
-
|
|
11
|
+
type PartialScriptTypeWithJson<T> = Partial<PromisedWithJson<T>>;
|
|
12
|
+
type GetPromisedServerScriptsOptions<T, IsJson extends boolean = boolean> = {
|
|
13
|
+
mockupFunctions?: PartialScriptType<T>;
|
|
14
|
+
parseJson?: IsJson;
|
|
15
|
+
};
|
|
16
|
+
declare function getPromisedServerScripts<T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, 'withSuccessHandler' | 'withFailureHandler' | 'withUserObject'>>(options: GetPromisedServerScriptsOptions<T, true>): PromisedWithJson<T>;
|
|
17
|
+
declare function getPromisedServerScripts<T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, 'withSuccessHandler' | 'withFailureHandler' | 'withUserObject'>>(options?: GetPromisedServerScriptsOptions<T, false>): Promised<T>;
|
|
6
18
|
|
|
7
19
|
export { getPromisedServerScripts };
|
|
8
|
-
export type { PartialScriptType, Promised };
|
|
20
|
+
export type { GetPromisedServerScriptsOptions, PartialScriptType, PartialScriptTypeWithJson, Promised, PromisedWithJson };
|
package/dist/promise.d.mts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
import { JsonString } from './json.mjs';
|
|
2
|
+
|
|
1
3
|
type Promised<T> = {
|
|
2
4
|
[K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<R> : T[K];
|
|
3
5
|
};
|
|
6
|
+
type UnwrapJson<T> = T extends JsonString<infer U> ? U : T;
|
|
7
|
+
type PromisedWithJson<T> = {
|
|
8
|
+
[K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<UnwrapJson<R>> : T[K];
|
|
9
|
+
};
|
|
4
10
|
type PartialScriptType<T> = Partial<Promised<T>>;
|
|
5
|
-
|
|
11
|
+
type PartialScriptTypeWithJson<T> = Partial<PromisedWithJson<T>>;
|
|
12
|
+
type GetPromisedServerScriptsOptions<T, IsJson extends boolean = boolean> = {
|
|
13
|
+
mockupFunctions?: PartialScriptType<T>;
|
|
14
|
+
parseJson?: IsJson;
|
|
15
|
+
};
|
|
16
|
+
declare function getPromisedServerScripts<T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, 'withSuccessHandler' | 'withFailureHandler' | 'withUserObject'>>(options: GetPromisedServerScriptsOptions<T, true>): PromisedWithJson<T>;
|
|
17
|
+
declare function getPromisedServerScripts<T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, 'withSuccessHandler' | 'withFailureHandler' | 'withUserObject'>>(options?: GetPromisedServerScriptsOptions<T, false>): Promised<T>;
|
|
6
18
|
|
|
7
19
|
export { getPromisedServerScripts };
|
|
8
|
-
export type { PartialScriptType, Promised };
|
|
20
|
+
export type { GetPromisedServerScriptsOptions, PartialScriptType, PartialScriptTypeWithJson, Promised, PromisedWithJson };
|
package/dist/promise.d.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
import { JsonString } from './json.js';
|
|
2
|
+
|
|
1
3
|
type Promised<T> = {
|
|
2
4
|
[K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<R> : T[K];
|
|
3
5
|
};
|
|
6
|
+
type UnwrapJson<T> = T extends JsonString<infer U> ? U : T;
|
|
7
|
+
type PromisedWithJson<T> = {
|
|
8
|
+
[K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<UnwrapJson<R>> : T[K];
|
|
9
|
+
};
|
|
4
10
|
type PartialScriptType<T> = Partial<Promised<T>>;
|
|
5
|
-
|
|
11
|
+
type PartialScriptTypeWithJson<T> = Partial<PromisedWithJson<T>>;
|
|
12
|
+
type GetPromisedServerScriptsOptions<T, IsJson extends boolean = boolean> = {
|
|
13
|
+
mockupFunctions?: PartialScriptType<T>;
|
|
14
|
+
parseJson?: IsJson;
|
|
15
|
+
};
|
|
16
|
+
declare function getPromisedServerScripts<T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, 'withSuccessHandler' | 'withFailureHandler' | 'withUserObject'>>(options: GetPromisedServerScriptsOptions<T, true>): PromisedWithJson<T>;
|
|
17
|
+
declare function getPromisedServerScripts<T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, 'withSuccessHandler' | 'withFailureHandler' | 'withUserObject'>>(options?: GetPromisedServerScriptsOptions<T, false>): Promised<T>;
|
|
6
18
|
|
|
7
19
|
export { getPromisedServerScripts };
|
|
8
|
-
export type { PartialScriptType, Promised };
|
|
20
|
+
export type { GetPromisedServerScriptsOptions, PartialScriptType, PartialScriptTypeWithJson, Promised, PromisedWithJson };
|
package/dist/promise.mjs
CHANGED
|
@@ -1,17 +1,47 @@
|
|
|
1
|
-
|
|
1
|
+
import { deserialize } from './json.mjs';
|
|
2
|
+
|
|
3
|
+
function getPromisedServerScripts(options = {}) {
|
|
4
|
+
const { mockupFunctions = {}, parseJson = false } = options;
|
|
2
5
|
return new Proxy(mockupFunctions, {
|
|
3
6
|
get(target, method) {
|
|
4
7
|
if (!("google" in globalThis) || !google?.script?.run) {
|
|
5
|
-
|
|
8
|
+
const mockFunc = target[method];
|
|
9
|
+
if (!mockFunc) {
|
|
10
|
+
return void 0;
|
|
11
|
+
}
|
|
12
|
+
if (!parseJson) {
|
|
13
|
+
return mockFunc;
|
|
14
|
+
}
|
|
15
|
+
return async (...args) => {
|
|
16
|
+
const res = await mockFunc(...args);
|
|
17
|
+
if (typeof res === "string") {
|
|
18
|
+
try {
|
|
19
|
+
return deserialize(res);
|
|
20
|
+
} catch {
|
|
21
|
+
return res;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return res;
|
|
25
|
+
};
|
|
6
26
|
}
|
|
7
27
|
if (!(method in google.script.run)) {
|
|
8
28
|
throw Error(`Method ${method} not found in AppsScript.`);
|
|
9
29
|
}
|
|
10
30
|
return (...args) => new Promise((resolve, reject) => {
|
|
11
|
-
google.script.run.withSuccessHandler(
|
|
31
|
+
google.script.run.withSuccessHandler((res) => {
|
|
32
|
+
if (parseJson && typeof res === "string") {
|
|
33
|
+
try {
|
|
34
|
+
resolve(deserialize(res));
|
|
35
|
+
} catch {
|
|
36
|
+
resolve(res);
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
resolve(res);
|
|
40
|
+
}
|
|
41
|
+
}).withFailureHandler(reject)[method](...args);
|
|
12
42
|
});
|
|
13
43
|
}
|
|
14
44
|
});
|
|
15
|
-
}
|
|
45
|
+
}
|
|
16
46
|
|
|
17
47
|
export { getPromisedServerScripts };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ciderjs/gasnuki",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Type definitions and utilities for Google Apps Script client-side API",
|
|
5
5
|
"main": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
"types": "./dist/vite.d.ts",
|
|
23
23
|
"import": "./dist/vite.mjs",
|
|
24
24
|
"require": "./dist/vite.cjs"
|
|
25
|
+
},
|
|
26
|
+
"./json": {
|
|
27
|
+
"types": "./dist/json.d.ts",
|
|
28
|
+
"import": "./dist/json.mjs",
|
|
29
|
+
"require": "./dist/json.cjs"
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
32
|
"scripts": {
|
|
@@ -45,7 +50,7 @@
|
|
|
45
50
|
"@google/clasp"
|
|
46
51
|
],
|
|
47
52
|
"author": "ciderjs/luth",
|
|
48
|
-
"license": "
|
|
53
|
+
"license": "MIT",
|
|
49
54
|
"repository": {
|
|
50
55
|
"type": "git",
|
|
51
56
|
"url": "git+ssh://git@github.com:luthpg/gasnuki.git"
|
|
@@ -62,12 +67,12 @@
|
|
|
62
67
|
"ts-morph": "^27.0.2"
|
|
63
68
|
},
|
|
64
69
|
"devDependencies": {
|
|
65
|
-
"@biomejs/biome": "^2.3.
|
|
66
|
-
"@types/node": "^25.0.
|
|
70
|
+
"@biomejs/biome": "^2.3.11",
|
|
71
|
+
"@types/node": "^25.0.5",
|
|
67
72
|
"@vitest/coverage-v8": "4.0.16",
|
|
68
73
|
"typescript": "^5.9.3",
|
|
69
74
|
"unbuild": "^3.6.1",
|
|
70
|
-
"vite": "^7.3.
|
|
75
|
+
"vite": "^7.3.1",
|
|
71
76
|
"vitest": "4.0.16"
|
|
72
77
|
},
|
|
73
78
|
"peerDependencies": {
|