Skip to content

Commit fc993bc

Browse files
committed
feat: add types
1 parent 2a8b4a3 commit fc993bc

File tree

3 files changed

+146
-3
lines changed

3 files changed

+146
-3
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"homepage": "https://github.com/posthtml/posthtml-fetch",
99
"repository": "posthtml/posthtml-fetch",
1010
"main": "lib",
11+
"types": "types/index.d.ts",
12+
"files": [
13+
"lib",
14+
"types"
15+
],
1116
"engines": {
1217
"node": ">=14.0.0"
1318
},
@@ -18,9 +23,6 @@
1823
"test": "vitest run --coverage",
1924
"lint": "biome lint ./lib ./test"
2025
},
21-
"files": [
22-
"lib"
23-
],
2426
"keywords": [
2527
"html",
2628
"posthtml",

types/expressions.d.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
export default interface ExpressionsConfig {
2+
/**
3+
Define the starting and ending delimiters used for expressions.
4+
5+
@default ['{{', '}}']
6+
*/
7+
delimiters?: string[];
8+
9+
/**
10+
Define the starting and ending delimiters used for unescaped expressions.
11+
12+
@default ['{{{', '}}}']
13+
*/
14+
unescapeDelimiters?: string[];
15+
16+
/**
17+
Object containing data that will be available under the `page` object.
18+
19+
@default {}
20+
*/
21+
locals?: Record<string, unknown>;
22+
23+
/**
24+
Attribute name for `<script>` tags that contain locals.
25+
26+
@default 'locals'
27+
*/
28+
localsAttr?: string;
29+
30+
/**
31+
Whether to remove `<script>` tags that contain locals.
32+
33+
@default false
34+
*/
35+
removeScriptLocals?: boolean;
36+
37+
/**
38+
Tag names to be used for if/else statements.
39+
40+
@default ['if', 'elseif', 'else']
41+
*/
42+
conditionalTags?: string[];
43+
44+
/**
45+
Tag names to be used for switch statements.
46+
47+
@default ['switch', 'case', 'default']
48+
*/
49+
switchTags?: string[];
50+
51+
/**
52+
Tag names to be used for loops.
53+
54+
@default ['each', 'for']
55+
*/
56+
loopTags?: string[];
57+
58+
/**
59+
Tag names to be used for scopes.
60+
61+
@default ['scope']
62+
*/
63+
scopeTags?: string[];
64+
65+
/**
66+
Name of tag inside of which expression parsing is disabled.
67+
68+
@default 'raw'
69+
*/
70+
ignoredTag?: string;
71+
72+
/**
73+
Enabling strict mode will throw an error if an expression cannot be evaluated.
74+
75+
@default false
76+
*/
77+
strictMode?: boolean;
78+
79+
/**
80+
What to render when referencing a value that is not defined in `locals`.
81+
82+
By default, the string 'undefined' will be output.
83+
84+
@default undefined
85+
86+
@example
87+
88+
```
89+
// Output empty string if value is not defined
90+
missingLocal: ''
91+
92+
// Output original reference if value is not defined
93+
missingLocal: '{local}'
94+
95+
// Output custom string if value is not defined
96+
missingLocal: 'ERR_NO_VALUE: {local}'
97+
```
98+
*/
99+
missingLocal?: string;
100+
}

types/index.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { $Fetch } from "ofetch";
2+
import type ExpressionsConfig from "./expressions";
3+
4+
export type PostHTMLFetchConfig = {
5+
/**
6+
String representing the attribute name containing the URL to fetch.
7+
8+
@default 'url'
9+
*/
10+
attribute?: string;
11+
/**
12+
`posthtml-expressions` is used to evaluate expressions.
13+
14+
You may pass options directly to it, inside the `expressions` object.
15+
16+
@default {}
17+
*/
18+
expressions?: ExpressionsConfig;
19+
/**
20+
`posthtml-fetch` uses `ofetch` to fetch data.
21+
22+
You may pass options directly to it, inside the `ofetch` object.
23+
24+
@default {}
25+
*/
26+
ofetch?: $Fetch;
27+
/**
28+
Set this to `true` if you need to preserve the `tag`, i.e. `<fetch>` around the response body.
29+
30+
@default false
31+
*/
32+
preserveTag?: boolean;
33+
/**
34+
Supported tag names.
35+
36+
Only tags from this array will be processed by the plugin.
37+
38+
@default ['fetch', 'remote']
39+
*/
40+
tags: string[];
41+
};

0 commit comments

Comments
 (0)