@ciderjs/gasnuki 0.4.1 → 0.5.0

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 CHANGED
@@ -1,9 +1,10 @@
1
1
  # @ciderjs/gasnuki
2
2
 
3
3
  [![README-en](https://img.shields.io/badge/English-blue?logo=ReadMe)](./README.md)
4
- [![Test Coverage](https://img.shields.io/badge/test%20coverage-93.02%25-brightgreen)](https://github.com/luthpg/gasnuki)
4
+ [![Test Coverage](https://img.shields.io/badge/test%20coverage-93.03%25-brightgreen)](https://github.com/luthpg/gasnuki)
5
5
  [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
6
6
  [![npm version](https://img.shields.io/npm/v/@ciderjs/gasnuki.svg)](https://www.npmjs.com/package/@ciderjs/gasnuki)
7
+ ![NPM Downloads](https://img.shields.io/npm/dw/@ciderjs/gasnuki)
7
8
  [![GitHub issues](https://img.shields.io/github/issues/luthpg/gasnuki.svg)](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 mockup: PartialScriptType<ServerScripts> = {
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>(mockup);
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](https://img.shields.io/badge/日本語-blue?logo=ReadMe)](./README.ja.md)
4
- [![Test Coverage](https://img.shields.io/badge/test%20coverage-93.02%25-brightgreen)](https://github.com/luthpg/gasnuki)
4
+ [![Test Coverage](https://img.shields.io/badge/test%20coverage-93.03%25-brightgreen)](https://github.com/luthpg/gasnuki)
5
5
  [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
6
6
  [![npm version](https://img.shields.io/npm/v/@ciderjs/gasnuki.svg)](https://www.npmjs.com/package/@ciderjs/gasnuki)
7
+ ![NPM Downloads](https://img.shields.io/npm/dw/@ciderjs/gasnuki)
7
8
  [![GitHub issues](https://img.shields.io/github/issues/luthpg/gasnuki.svg)](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 mockup: PartialScriptType<ServerScripts> = {
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>(mockup);
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
@@ -26,7 +26,7 @@ function _interopNamespaceCompat(e) {
26
26
 
27
27
  const path__namespace = /*#__PURE__*/_interopNamespaceCompat(path);
28
28
 
29
- const version = "0.4.1";
29
+ const version = "0.5.0";
30
30
 
31
31
  const parseArgs = async (command) => {
32
32
  const cliOpts = command.opts();
package/dist/cli.mjs CHANGED
@@ -10,7 +10,7 @@ import 'node:fs';
10
10
  import 'ts-morph';
11
11
  import 'jiti';
12
12
 
13
- const version = "0.4.1";
13
+ const version = "0.5.0";
14
14
 
15
15
  const parseArgs = async (command) => {
16
16
  const cliOpts = command.opts();
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;
@@ -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 };
@@ -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,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const getPromisedServerScripts = (mockupFunctions = {}) => {
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) {
@@ -10,10 +13,20 @@ const getPromisedServerScripts = (mockupFunctions = {}) => {
10
13
  throw Error(`Method ${method} not found in AppsScript.`);
11
14
  }
12
15
  return (...args) => new Promise((resolve, reject) => {
13
- google.script.run.withSuccessHandler(resolve).withFailureHandler(reject)[method](...args);
16
+ google.script.run.withSuccessHandler((res) => {
17
+ if (parseJson && typeof res === "string") {
18
+ try {
19
+ resolve(json.deserialize(res));
20
+ } catch {
21
+ resolve(res);
22
+ }
23
+ } else {
24
+ resolve(res);
25
+ }
26
+ }).withFailureHandler(reject)[method](...args);
14
27
  });
15
28
  }
16
29
  });
17
- };
30
+ }
18
31
 
19
32
  exports.getPromisedServerScripts = getPromisedServerScripts;
@@ -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
- declare const getPromisedServerScripts: <T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, "withSuccessHandler" | "withFailureHandler" | "withUserObject">>(mockupFunctions?: PartialScriptType<T>) => Promised<T>;
11
+ type PartialScriptTypeWithJson<T> = Partial<PromisedWithJson<T>>;
12
+ type GetPromisedServerScriptsOptions<T, IsJson extends boolean = boolean> = {
13
+ mockupFunctions?: IsJson extends true ? PartialScriptTypeWithJson<T> : 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 };
@@ -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
- declare const getPromisedServerScripts: <T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, "withSuccessHandler" | "withFailureHandler" | "withUserObject">>(mockupFunctions?: PartialScriptType<T>) => Promised<T>;
11
+ type PartialScriptTypeWithJson<T> = Partial<PromisedWithJson<T>>;
12
+ type GetPromisedServerScriptsOptions<T, IsJson extends boolean = boolean> = {
13
+ mockupFunctions?: IsJson extends true ? PartialScriptTypeWithJson<T> : 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
- declare const getPromisedServerScripts: <T extends Record<string, (...args: any[]) => any> = Omit<typeof google.script.run, "withSuccessHandler" | "withFailureHandler" | "withUserObject">>(mockupFunctions?: PartialScriptType<T>) => Promised<T>;
11
+ type PartialScriptTypeWithJson<T> = Partial<PromisedWithJson<T>>;
12
+ type GetPromisedServerScriptsOptions<T, IsJson extends boolean = boolean> = {
13
+ mockupFunctions?: IsJson extends true ? PartialScriptTypeWithJson<T> : 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,4 +1,7 @@
1
- const getPromisedServerScripts = (mockupFunctions = {}) => {
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) {
@@ -8,10 +11,20 @@ const getPromisedServerScripts = (mockupFunctions = {}) => {
8
11
  throw Error(`Method ${method} not found in AppsScript.`);
9
12
  }
10
13
  return (...args) => new Promise((resolve, reject) => {
11
- google.script.run.withSuccessHandler(resolve).withFailureHandler(reject)[method](...args);
14
+ google.script.run.withSuccessHandler((res) => {
15
+ if (parseJson && typeof res === "string") {
16
+ try {
17
+ resolve(deserialize(res));
18
+ } catch {
19
+ resolve(res);
20
+ }
21
+ } else {
22
+ resolve(res);
23
+ }
24
+ }).withFailureHandler(reject)[method](...args);
12
25
  });
13
26
  }
14
27
  });
15
- };
28
+ }
16
29
 
17
30
  export { getPromisedServerScripts };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ciderjs/gasnuki",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
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": "ISC",
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.10",
66
- "@types/node": "^25.0.3",
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.0",
75
+ "vite": "^7.3.1",
71
76
  "vitest": "4.0.16"
72
77
  },
73
78
  "peerDependencies": {