sendscript 2.4.2 → 2.4.3
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/CHANGELOG.md +6 -0
- package/README.md +93 -21
- package/README.mz +45 -17
- package/example/typescript/client.ts +16 -3
- package/example/typescript/math.client.ts +10 -1
- package/example/typescript/math.ts +9 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,8 +4,14 @@ All notable changes to this project will be documented in this file. Dates are d
|
|
|
4
4
|
|
|
5
5
|
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
|
|
6
6
|
|
|
7
|
+
#### [v2.4.3](https://github.com/bas080/sendscript/compare/v2.4.2...v2.4.3)
|
|
8
|
+
|
|
9
|
+
- Improve TypeScript section with comprehensive examples and explanations [`a3c5867`](https://github.com/bas080/sendscript/commit/a3c5867f835bbcde1e802e75c80ca6f02dd4f0e8)
|
|
10
|
+
|
|
7
11
|
#### [v2.4.2](https://github.com/bas080/sendscript/compare/v2.4.1...v2.4.2)
|
|
8
12
|
|
|
13
|
+
> 17 August 2026
|
|
14
|
+
|
|
9
15
|
- Update tap and typedoc [`46421de`](https://github.com/bas080/sendscript/commit/46421de16533066ee13c91ea75505b1ecdd548f0)
|
|
10
16
|
- Remove the unused curry helper [`9dcbb48`](https://github.com/bas080/sendscript/commit/9dcbb485b455937070c65359ecde8236c128bee8)
|
|
11
17
|
- Remove duplicate Promises section with outdated async/await information [`5f43ddb`](https://github.com/bas080/sendscript/commit/5f43ddb91ddb2d77227ddd6a570807f874230ea1)
|
package/README.md
CHANGED
|
@@ -36,6 +36,10 @@ Serialize and execute composable JavaScript function calls with JSON.
|
|
|
36
36
|
* [.then / .catch](#then--catch)
|
|
37
37
|
* [await](#await)
|
|
38
38
|
- [TypeScript](#typescript)
|
|
39
|
+
* [Server-Side Module](#server-side-module)
|
|
40
|
+
* [Client-Side Type Stub](#client-side-type-stub)
|
|
41
|
+
* [Using Typed References](#using-typed-references)
|
|
42
|
+
* [Generating API Documentation](#generating-api-documentation)
|
|
39
43
|
- [Schema and Nested Modules](#schema-and-nested-modules)
|
|
40
44
|
* [Defining a Nested Module](#defining-a-nested-module)
|
|
41
45
|
- [Validation (using Zod)](#validation-using-zod)
|
|
@@ -326,37 +330,84 @@ defined properties and returned values.
|
|
|
326
330
|
|
|
327
331
|
## TypeScript
|
|
328
332
|
|
|
329
|
-
|
|
333
|
+
Using SendScript with TypeScript enables **type-safe client-side code**. Your
|
|
334
|
+
client can have full IDE autocomplete and compile-time type checking when
|
|
335
|
+
calling server functions.
|
|
330
336
|
|
|
331
|
-
|
|
332
|
-
coding.
|
|
333
|
-
2. You can use tools like [typedoc][typedoc] to generate docs from your types to
|
|
334
|
-
share with consumers of your API.
|
|
335
|
-
3. You can use the types of the module to coerce your client to adopt the
|
|
336
|
-
module's type.
|
|
337
|
+
### Server-Side Module
|
|
337
338
|
|
|
338
|
-
|
|
339
|
+
Define your API as a TypeScript module on the server:
|
|
339
340
|
|
|
340
341
|
```bash
|
|
341
342
|
cat ./example/typescript/math.ts
|
|
342
343
|
```
|
|
343
344
|
```ts
|
|
344
|
-
|
|
345
|
-
|
|
345
|
+
/**
|
|
346
|
+
* Server-side math module with typed functions
|
|
347
|
+
* These functions will be called from the client through SendScript
|
|
348
|
+
*/
|
|
349
|
+
|
|
350
|
+
export const add = (a: number, b: number): number => a + b
|
|
351
|
+
|
|
352
|
+
export const square = (a: number): number => a * a
|
|
353
|
+
|
|
346
354
|
```
|
|
347
355
|
|
|
348
|
-
|
|
356
|
+
### Client-Side Type Stub
|
|
357
|
+
|
|
358
|
+
Create a client-side file that mirrors your server types using the `as typeof`
|
|
359
|
+
casting pattern:
|
|
360
|
+
|
|
361
|
+
```bash
|
|
362
|
+
cat ./example/typescript/math.client.ts
|
|
363
|
+
```
|
|
364
|
+
```ts
|
|
365
|
+
/**
|
|
366
|
+
* Client-side type-safe stubs for the math API
|
|
367
|
+
*
|
|
368
|
+
* This file creates typed references that mirror the server's functions.
|
|
369
|
+
* The 'as typeof mathTypes' cast gives us full TypeScript support and IDE autocomplete.
|
|
370
|
+
*/
|
|
371
|
+
|
|
372
|
+
import type * as mathTypes from './math.ts'
|
|
373
|
+
import references from 'sendscript/references.mjs'
|
|
374
|
+
|
|
375
|
+
// Create type-safe stubs - this tells TypeScript that 'add' and 'square'
|
|
376
|
+
// have the same signatures as the server functions
|
|
377
|
+
export default references(['add', 'square']) as typeof mathTypes
|
|
378
|
+
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
The `as typeof mathTypes` type assertion gives your client-side references the
|
|
382
|
+
exact same types as your server module. This means:
|
|
383
|
+
|
|
384
|
+
- Full IDE autocomplete for function names and parameters
|
|
385
|
+
- Compile-time type checking - catch errors before runtime
|
|
386
|
+
- Your client code looks identical to regular JavaScript calls
|
|
387
|
+
|
|
388
|
+
### Using Typed References
|
|
389
|
+
|
|
390
|
+
Now on your client, you have complete type safety:
|
|
349
391
|
|
|
350
392
|
```bash
|
|
351
393
|
cat ./example/typescript/client.ts
|
|
352
394
|
```
|
|
353
395
|
```ts
|
|
354
|
-
|
|
396
|
+
/**
|
|
397
|
+
* Client-side usage with type-safe SendScript
|
|
398
|
+
*/
|
|
399
|
+
|
|
400
|
+
import math from './math.client.ts'
|
|
355
401
|
import Stringify from 'sendscript/stringify.mjs'
|
|
356
402
|
|
|
357
403
|
const stringify = Stringify()
|
|
358
404
|
|
|
359
|
-
|
|
405
|
+
/**
|
|
406
|
+
* Send a SendScript program to the server
|
|
407
|
+
*
|
|
408
|
+
* TypeScript knows that the return type matches the program's return type.
|
|
409
|
+
* In this case, square(add(1, 2)) returns a number, so T is number.
|
|
410
|
+
*/
|
|
360
411
|
async function send<T>(program: T): Promise<T> {
|
|
361
412
|
return (await fetch('/api', {
|
|
362
413
|
method: 'POST',
|
|
@@ -364,20 +415,41 @@ async function send<T>(program: T): Promise<T> {
|
|
|
364
415
|
})).json()
|
|
365
416
|
}
|
|
366
417
|
|
|
367
|
-
|
|
418
|
+
// TypeScript provides full autocomplete for math.add and math.square
|
|
419
|
+
// It knows they take numbers and return numbers
|
|
420
|
+
const result = await send(math.square(math.add(1, 2)))
|
|
421
|
+
console.log(result) // 9
|
|
422
|
+
|
|
368
423
|
```
|
|
369
424
|
|
|
370
|
-
|
|
425
|
+
TypeScript knows the exact parameter types and return types for every function
|
|
426
|
+
call.
|
|
427
|
+
|
|
428
|
+
### Generating API Documentation
|
|
429
|
+
|
|
430
|
+
You can use [typedoc][typedoc] to automatically generate documentation from your
|
|
431
|
+
TypeScript types:
|
|
371
432
|
|
|
372
433
|
```bash
|
|
373
|
-
npx typedoc --plugin typedoc-plugin-markdown --out ./
|
|
434
|
+
npx typedoc --plugin typedoc-plugin-markdown --out ./docs ./example/typescript/math.ts
|
|
435
|
+
```
|
|
436
|
+
```
|
|
437
|
+
[96m[info][0m Loaded plugin typedoc-plugin-markdown
|
|
438
|
+
[96m[info][0m markdown generated at ./docs
|
|
374
439
|
```
|
|
375
440
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
>
|
|
380
|
-
>
|
|
441
|
+
This generates markdown docs that can be shared with API consumers. See the
|
|
442
|
+
[generated docs](./example/typescript/docs/globals.md).
|
|
443
|
+
|
|
444
|
+
> [!IMPORTANT] **Type vs. Runtime Values**
|
|
445
|
+
>
|
|
446
|
+
> Type casting on the client side improves the development experience, but
|
|
447
|
+
> remember:
|
|
448
|
+
>
|
|
449
|
+
> - The actual serialized JSON may differ from the static types
|
|
450
|
+
> - Runtime values depend on serialization/deserialization
|
|
451
|
+
> - Always validate user input on the server using schema validation (e.g., Zod)
|
|
452
|
+
> - Use the types as a contract, not a guarantee
|
|
381
453
|
|
|
382
454
|
## Schema and Nested Modules
|
|
383
455
|
|
package/README.mz
CHANGED
|
@@ -285,38 +285,66 @@ defined properties and returned values.
|
|
|
285
285
|
|
|
286
286
|
## TypeScript
|
|
287
287
|
|
|
288
|
-
|
|
288
|
+
Using SendScript with TypeScript enables **type-safe client-side code**. Your
|
|
289
|
+
client can have full IDE autocomplete and compile-time type checking when
|
|
290
|
+
calling server functions.
|
|
289
291
|
|
|
290
|
-
|
|
291
|
-
coding.
|
|
292
|
-
2. You can use tools like [typedoc][typedoc] to generate docs from your types to
|
|
293
|
-
share with consumers of your API.
|
|
294
|
-
3. You can use the types of the module to coerce your client to adopt the
|
|
295
|
-
module's type.
|
|
292
|
+
### Server-Side Module
|
|
296
293
|
|
|
297
|
-
|
|
294
|
+
Define your API as a TypeScript module on the server:
|
|
298
295
|
|
|
299
296
|
```bash|ts bash
|
|
300
297
|
cat ./example/typescript/math.ts
|
|
301
298
|
```
|
|
302
299
|
|
|
303
|
-
|
|
300
|
+
### Client-Side Type Stub
|
|
301
|
+
|
|
302
|
+
Create a client-side file that mirrors your server types using the `as typeof`
|
|
303
|
+
casting pattern:
|
|
304
304
|
|
|
305
305
|
```bash|ts bash
|
|
306
|
-
cat ./example/typescript/client.ts
|
|
306
|
+
cat ./example/typescript/math.client.ts
|
|
307
307
|
```
|
|
308
308
|
|
|
309
|
-
|
|
309
|
+
The `as typeof mathTypes` type assertion gives your client-side references the
|
|
310
|
+
exact same types as your server module. This means:
|
|
311
|
+
|
|
312
|
+
- Full IDE autocomplete for function names and parameters
|
|
313
|
+
- Compile-time type checking - catch errors before runtime
|
|
314
|
+
- Your client code looks identical to regular JavaScript calls
|
|
310
315
|
|
|
311
|
-
|
|
312
|
-
|
|
316
|
+
### Using Typed References
|
|
317
|
+
|
|
318
|
+
Now on your client, you have complete type safety:
|
|
319
|
+
|
|
320
|
+
```bash|ts bash
|
|
321
|
+
cat ./example/typescript/client.ts
|
|
313
322
|
```
|
|
314
323
|
|
|
315
|
-
|
|
324
|
+
TypeScript knows the exact parameter types and return types for every function
|
|
325
|
+
call.
|
|
326
|
+
|
|
327
|
+
### Generating API Documentation
|
|
328
|
+
|
|
329
|
+
You can use [typedoc][typedoc] to automatically generate documentation from your
|
|
330
|
+
TypeScript types:
|
|
331
|
+
|
|
332
|
+
```bash bash
|
|
333
|
+
npx typedoc --plugin typedoc-plugin-markdown --out ./docs ./example/typescript/math.ts
|
|
334
|
+
```
|
|
316
335
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
336
|
+
This generates markdown docs that can be shared with API consumers. See the
|
|
337
|
+
[generated docs](./example/typescript/docs/globals.md).
|
|
338
|
+
|
|
339
|
+
> [!IMPORTANT] **Type vs. Runtime Values**
|
|
340
|
+
>
|
|
341
|
+
> Type casting on the client side improves the development experience, but
|
|
342
|
+
> remember:
|
|
343
|
+
>
|
|
344
|
+
> - The actual serialized JSON may differ from the static types
|
|
345
|
+
> - Runtime values depend on serialization/deserialization
|
|
346
|
+
> - Always validate user input on the server using schema validation (e.g., Zod)
|
|
347
|
+
> - Use the types as a contract, not a guarantee
|
|
320
348
|
|
|
321
349
|
## Schema and Nested Modules
|
|
322
350
|
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Client-side usage with type-safe SendScript
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import math from './math.client.ts'
|
|
2
6
|
import Stringify from 'sendscript/stringify.mjs'
|
|
3
7
|
|
|
4
8
|
const stringify = Stringify()
|
|
5
9
|
|
|
6
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Send a SendScript program to the server
|
|
12
|
+
*
|
|
13
|
+
* TypeScript knows that the return type matches the program's return type.
|
|
14
|
+
* In this case, square(add(1, 2)) returns a number, so T is number.
|
|
15
|
+
*/
|
|
7
16
|
async function send<T>(program: T): Promise<T> {
|
|
8
17
|
return (await fetch('/api', {
|
|
9
18
|
method: 'POST',
|
|
@@ -11,4 +20,8 @@ async function send<T>(program: T): Promise<T> {
|
|
|
11
20
|
})).json()
|
|
12
21
|
}
|
|
13
22
|
|
|
14
|
-
|
|
23
|
+
// TypeScript provides full autocomplete for math.add and math.square
|
|
24
|
+
// It knows they take numbers and return numbers
|
|
25
|
+
const result = await send(math.square(math.add(1, 2)))
|
|
26
|
+
console.log(result) // 9
|
|
27
|
+
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side type-safe stubs for the math API
|
|
3
|
+
*
|
|
4
|
+
* This file creates typed references that mirror the server's functions.
|
|
5
|
+
* The 'as typeof mathTypes' cast gives us full TypeScript support and IDE autocomplete.
|
|
6
|
+
*/
|
|
7
|
+
|
|
1
8
|
import type * as mathTypes from './math.ts'
|
|
2
|
-
import Stringify from 'sendscript/stringify.mjs'
|
|
3
9
|
import references from 'sendscript/references.mjs'
|
|
4
10
|
|
|
11
|
+
// Create type-safe stubs - this tells TypeScript that 'add' and 'square'
|
|
12
|
+
// have the same signatures as the server functions
|
|
5
13
|
export default references(['add', 'square']) as typeof mathTypes
|
|
14
|
+
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Server-side math module with typed functions
|
|
3
|
+
* These functions will be called from the client through SendScript
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const add = (a: number, b: number): number => a + b
|
|
7
|
+
|
|
8
|
+
export const square = (a: number): number => a * a
|
|
9
|
+
|