spooder 6.2.1 → 6.2.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/README.md +28 -0
- package/package.json +1 -1
- package/src/api.ts +28 -3
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
package/src/api.ts
CHANGED
|
@@ -977,8 +977,29 @@ export async function parse_template(template: string, replacements: Replacement
|
|
|
977
977
|
// Parse t-if tags
|
|
978
978
|
const if_regex = /<t-if\s+test="([^"]+)"\s*>(.*?)<\/t-if>/gs;
|
|
979
979
|
result = await replace_async(result, if_regex, async (match, condition_key, if_content) => {
|
|
980
|
-
|
|
981
|
-
|
|
980
|
+
let condition_value;
|
|
981
|
+
let key_exists = false;
|
|
982
|
+
|
|
983
|
+
if (is_replacer_fn) {
|
|
984
|
+
condition_value = await replacements(condition_key);
|
|
985
|
+
key_exists = true;
|
|
986
|
+
} else {
|
|
987
|
+
condition_value = replacements[condition_key];
|
|
988
|
+
key_exists = condition_key in replacements;
|
|
989
|
+
|
|
990
|
+
// try nested property access if direct lookup fails
|
|
991
|
+
if (condition_value === undefined && condition_key.includes('.')) {
|
|
992
|
+
const dot_index = condition_key.indexOf('.');
|
|
993
|
+
const base_key = condition_key.substring(0, dot_index);
|
|
994
|
+
const prop_path = condition_key.substring(dot_index + 1);
|
|
995
|
+
const base_obj = replacements[base_key];
|
|
996
|
+
|
|
997
|
+
if (base_obj !== undefined && typeof base_obj === 'object' && base_obj !== null) {
|
|
998
|
+
condition_value = get_nested_property(base_obj, prop_path);
|
|
999
|
+
key_exists = base_key in replacements;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
982
1003
|
|
|
983
1004
|
// preserve block for later pass if key doesn't exist and drop_missing is false
|
|
984
1005
|
if (!key_exists && !drop_missing)
|
|
@@ -1040,8 +1061,12 @@ export async function parse_template(template: string, replacements: Replacement
|
|
|
1040
1061
|
replacement = await replacement();
|
|
1041
1062
|
}
|
|
1042
1063
|
|
|
1043
|
-
if (replacement !== undefined)
|
|
1064
|
+
if (replacement !== undefined) {
|
|
1065
|
+
if (typeof replacement === 'object' && replacement !== null)
|
|
1066
|
+
return JSON.stringify(replacement);
|
|
1067
|
+
|
|
1044
1068
|
return replacement;
|
|
1069
|
+
}
|
|
1045
1070
|
|
|
1046
1071
|
if (!drop_missing)
|
|
1047
1072
|
return match;
|