@eik/node-client 2.0.0-next.5 → 2.0.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.
@@ -0,0 +1,218 @@
1
+ /**
2
+ * @typedef {object} Options
3
+ * @property {string} [base=null]
4
+ * @property {boolean} [development=false]
5
+ * @property {boolean} [loadMaps=false]
6
+ * @property {string} [path=process.cwd()]
7
+ */
8
+ /**
9
+ * @typedef {object} ImportMap
10
+ * @property {Record<string, string>} imports
11
+ */
12
+ /**
13
+ * An Eik utility for servers running on Node. With it you can:
14
+ * - generate different URLs to assets on an Eik server depending on environment (development vs production).
15
+ * - get the import maps you have configured in `eik.json` from the Eik server, should you want to use them in the HTML response.
16
+ *
17
+ * @example
18
+ * ```js
19
+ * // Create an instance, then load information from `eik.json` and the Eik server
20
+ * import Eik from "@eik/node-client";
21
+ *
22
+ * const eik = new Eik();
23
+ * await eik.load();
24
+ * ```
25
+ * @example
26
+ * ```js
27
+ * // Serve a local version of a file from `./public`
28
+ * // in development and from Eik in production
29
+ * import path from "node:path";
30
+ * import Eik from "@eik/node-client";
31
+ * import fastifyStatic from "@fastify/static";
32
+ * import fastify from "fastify";
33
+ *
34
+ * const app = fastify();
35
+ * app.register(fastifyStatic, {
36
+ * root: path.join(process.cwd(), "public"),
37
+ * prefix: "/public/",
38
+ * });
39
+ *
40
+ * const eik = new Eik({
41
+ * development: process.env.NODE_ENV === "development",
42
+ * base: "/public",
43
+ * });
44
+ *
45
+ * // load information from `eik.json` and the Eik server
46
+ * await eik.load();
47
+ *
48
+ * // when development is true script.value will be /public/script.js.
49
+ * // when development is false script.value will be
50
+ * // https://{server}/pkg/{name}/{version}/script.js
51
+ * // where {server}, {name} and {version} are read from eik.json
52
+ * const script = eik.file("/script.js");
53
+ *
54
+ * app.get("/", (req, reply) => {
55
+ * reply.type("text/html; charset=utf-8");
56
+ * reply.send(`<html><body>
57
+ * <script
58
+ * src="${script.value}"
59
+ * ${script.integrity ? `integrity="${script.integrity}"` : ""}
60
+ * type="module"></script>
61
+ * </body></html>`);
62
+ * });
63
+ *
64
+ * app.listen({
65
+ * port: 3000,
66
+ * });
67
+ *
68
+ * console.log("Listening on http://localhost:3000");
69
+ * ```
70
+ */
71
+ export default class Eik {
72
+ /**
73
+ * @param {Options} options
74
+ */
75
+ constructor({ development, loadMaps, base, path, }?: Options);
76
+ /**
77
+ * Reads the Eik config from disk into the object instance, used for building {@link file} links in production.
78
+ *
79
+ * If {@link Options.loadMaps} is `true` the import maps
80
+ * defined in the Eik config will be fetched from the Eik server for
81
+ * use in {@link maps}.
82
+ */
83
+ load(): Promise<void>;
84
+ /**
85
+ * The `"name"` field from the Eik config
86
+ * @throws if read before calling {@link load}
87
+ */
88
+ get name(): any;
89
+ /**
90
+ * The `"version"` field from the Eik config
91
+ * @throws if read before calling {@link load}
92
+ */
93
+ get version(): any;
94
+ /**
95
+ * The `"type"` field from the Eik config mapped to its URL equivalent (eg. "package" is "pkg").
96
+ * @throws if read before calling {@link load}
97
+ */
98
+ get type(): any;
99
+ /**
100
+ * The `"server"` field from the Eik config
101
+ * @throws if read before calling {@link load}
102
+ */
103
+ get server(): any;
104
+ /**
105
+ * The pathname to the base on Eik (ex. /pkg/my-app/1.0.0/)
106
+ * @throws if read before calling {@link load}
107
+ */
108
+ get pathname(): string;
109
+ /**
110
+ * Similar to {@link file}, this method returns a path to the base on Eik
111
+ * (ex. https://eik.store.com/pkg/my-app/1.0.0), or {@link Options.base}
112
+ * if {@link Options.development} is true.
113
+ *
114
+ * You can use this instead of `file` if you have a directory full of files
115
+ * and you don't need {@link Asset.integrity}.
116
+ *
117
+ * @returns {string} The base path for assets published on Eik
118
+ * @throws when {@link Options.development} is false if called before calling {@link load}
119
+ */
120
+ base(): string;
121
+ /**
122
+ * Get a link to a file that is published on Eik when running in production.
123
+ * When {@link Options.development} is `true` the pathname is prefixed
124
+ * with the {@link Options.base} option instead of pointing to Eik.
125
+ *
126
+ * @param {string} pathname pathname to the file relative to the base on Eik (ex: /path/to/script.js for a prod URL https://eik.store.com/pkg/my-app/1.0.0/path/to/script.js)
127
+ * @returns {import('./asset.js').Asset}
128
+ * @throws when {@link Options.development} is false if called before calling {@link load}
129
+ *
130
+ * @example
131
+ * ```js
132
+ * // in production
133
+ * const eik = new Eik({
134
+ * development: false,
135
+ * });
136
+ * await eik.load();
137
+ *
138
+ * const file = eik.file("/path/to/script.js");
139
+ * // {
140
+ * // value: https://eik.store.com/pkg/my-app/1.0.0/path/to/script.js
141
+ * // integrity: sha512-zHQjnD-etc.
142
+ * // }
143
+ * // where the server URL, app name and version are read from eik.json
144
+ * // {
145
+ * // "name": "my-app",
146
+ * // "version": "1.0.0",
147
+ * // "server": "https://eik.store.com",
148
+ * // }
149
+ * ```
150
+ * @example
151
+ * ```js
152
+ * // in development
153
+ * const eik = new Eik({
154
+ * development: true,
155
+ * base: "/public",
156
+ * });
157
+ * await eik.load();
158
+ *
159
+ * const file = eik.file("/path/to/script.js");
160
+ * // {
161
+ * // value: /public/path/to/script.js
162
+ * // integrity: undefined
163
+ * // }
164
+ * ```
165
+ */
166
+ file(pathname?: string): import("./asset.js").Asset;
167
+ /**
168
+ * When {@link Options.loadMaps} is `true` and you call {@link load}, the client
169
+ * fetches the configured import maps from the Eik server.
170
+ *
171
+ * This method returns the import maps that were fetched during `load`.
172
+ *
173
+ * @returns {ImportMap[]}
174
+ * @throws if {@link Options.loadMaps} is not `true` or called before calling {@link load}
175
+ *
176
+ * @example
177
+ * ```js
178
+ * const client = new Eik({
179
+ * loadMaps: true,
180
+ * });
181
+ * await client.load();
182
+ *
183
+ * const maps = client.maps();
184
+ * ```
185
+ */
186
+ maps(): ImportMap[];
187
+ /**
188
+ * Function that generates and returns an import map script tag for use in an document head.
189
+ *
190
+ * Only a single import map is allowed per HTML document.
191
+ * A key (ex. `react`) can only be defined once.
192
+ * If multiple import maps defined in `eik.json` use the same key, the last key wins.
193
+ *
194
+ * @example
195
+ * ```
196
+ * const importMap = eik.toHTML();
197
+ *
198
+ * <head>
199
+ * ...
200
+ * ${importMap}
201
+ * ...
202
+ * </head>
203
+ * ```
204
+ *
205
+ * @returns {string}
206
+ */
207
+ toHTML(): string;
208
+ #private;
209
+ }
210
+ export type Options = {
211
+ base?: string;
212
+ development?: boolean;
213
+ loadMaps?: boolean;
214
+ path?: string;
215
+ };
216
+ export type ImportMap = {
217
+ imports: Record<string, string>;
218
+ };