spooder 6.2.1 → 6.2.2

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.
Files changed (3) hide show
  1. package/README.md +28 -0
  2. package/package.json +1 -1
  3. package/src/api.ts +5 -1
package/README.md CHANGED
@@ -2520,6 +2520,34 @@ await parse_template(..., {
2520
2520
  </t-for>
2521
2521
  ```
2522
2522
 
2523
+ #### Object Serialization
2524
+
2525
+ When a replacement value is an object or array, it is automatically serialized to JSON. This allows server-side data to be embedded directly into client-side scripts.
2526
+
2527
+ ```ts
2528
+ const config = {
2529
+ debug: true,
2530
+ api_url: '/api/v1',
2531
+ features: ['auth', 'logging']
2532
+ };
2533
+
2534
+ await parse_template('<script>const CONFIG = {{config}};</script>', { config });
2535
+ // Result: "<script>const CONFIG = {"debug":true,"api_url":"/api/v1","features":["auth","logging"]};</script>"
2536
+ ```
2537
+
2538
+ This also works with nested objects accessed via dot notation:
2539
+
2540
+ ```ts
2541
+ const data = {
2542
+ app: {
2543
+ settings: { theme: 'dark', lang: 'en' }
2544
+ }
2545
+ };
2546
+
2547
+ await parse_template('<script>const SETTINGS = {{app.settings}};</script>', data);
2548
+ // Result: "<script>const SETTINGS = {"theme":"dark","lang":"en"};</script>"
2549
+ ```
2550
+
2523
2551
  <a id="api-cache-busting"></a>
2524
2552
  ## API > Cache Busting
2525
2553
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "spooder",
3
3
  "author": "Kruithne <kruithne@gmail.com>",
4
4
  "type": "module",
5
- "version": "6.2.1",
5
+ "version": "6.2.2",
6
6
  "module": "./src/api.ts",
7
7
  "repository": {
8
8
  "url": "https://github.com/Kruithne/spooder"
package/src/api.ts CHANGED
@@ -1040,8 +1040,12 @@ export async function parse_template(template: string, replacements: Replacement
1040
1040
  replacement = await replacement();
1041
1041
  }
1042
1042
 
1043
- if (replacement !== undefined)
1043
+ if (replacement !== undefined) {
1044
+ if (typeof replacement === 'object' && replacement !== null)
1045
+ return JSON.stringify(replacement);
1046
+
1044
1047
  return replacement;
1048
+ }
1045
1049
 
1046
1050
  if (!drop_missing)
1047
1051
  return match;