@wayke-se/ev-web 0.1.1 → 1.0.0-beta.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 (36) hide show
  1. package/README.md +254 -81
  2. package/dist/api.d.ts +50 -0
  3. package/dist/chargingCost.d.ts +28 -0
  4. package/dist/dom.d.ts +14 -0
  5. package/dist/i18n.d.ts +46 -0
  6. package/dist/index.d.ts +2 -567
  7. package/dist/index.js +14 -358
  8. package/dist/index.js.map +4 -4
  9. package/dist/mount.d.ts +9 -0
  10. package/dist/style.css +2 -0
  11. package/dist/style.css.map +7 -0
  12. package/dist/types.d.ts +105 -0
  13. package/dist/widget.d.ts +12 -0
  14. package/package.json +59 -38
  15. package/dist/GeneralSans-Bold-B2L2RQWE.woff +0 -0
  16. package/dist/GeneralSans-Bold-VZ4Y7PGT.woff2 +0 -0
  17. package/dist/GeneralSans-Regular-5M4NQDY6.woff2 +0 -0
  18. package/dist/GeneralSans-Regular-6E2LFRI2.woff +0 -0
  19. package/dist/GeneralSans-Semibold-LBITB72G.woff +0 -0
  20. package/dist/GeneralSans-Semibold-XUXDDWPR.woff2 +0 -0
  21. package/dist/OpenSauceOne-ExtraBold-3ZSFGTY7.woff2 +0 -0
  22. package/dist/OpenSauceOne-ExtraBold-HMQJUXJT.woff +0 -0
  23. package/dist/OpenSauceOne-Regular-2D5N6QGZ.woff2 +0 -0
  24. package/dist/OpenSauceOne-Regular-F62Q4XLM.woff +0 -0
  25. package/dist/StyreneA-Bold-Web-4CXEYQLA.woff2 +0 -0
  26. package/dist/StyreneA-Bold-Web-AGBUOKS2.woff +0 -0
  27. package/dist/StyreneA-Light-Web-DAMPSUFB.woff +0 -0
  28. package/dist/StyreneA-Light-Web-EE4AJECE.woff2 +0 -0
  29. package/dist/StyreneA-Medium-Web-53Q6X35A.woff +0 -0
  30. package/dist/StyreneA-Medium-Web-7KYQNCKQ.woff2 +0 -0
  31. package/dist/StyreneA-Regular-Web-7SAOGHB5.woff2 +0 -0
  32. package/dist/StyreneA-Regular-Web-XFBCXNPA.woff +0 -0
  33. package/dist/index.css +0 -2
  34. package/dist/index.css.map +0 -7
  35. package/dist/index.mjs +0 -359
  36. package/dist/index.mjs.map +0 -7
package/README.md CHANGED
@@ -1,123 +1,296 @@
1
1
  # Wayke EV Web
2
2
 
3
- This is an embeddable widget used to calculate electric car charging costs.
3
+ A calculator that shows what it costs to charge an electric car from Wayke, per month. The visitor picks a yearly distance and can use their own electricity price. Made for the vehicle page of a dealer or partner website.
4
4
 
5
- ## Getting started
5
+ Upgrading from 0.x? See [Migrating from 0.x to 1.0](#migrating-from-0x-to-10).
6
6
 
7
- __Using npm__
7
+ ## Quick start
8
+
9
+ Add this where the calculator should be shown, and replace `<vehicle id>` with the id of the vehicle's ad at Wayke:
10
+
11
+ ```html
12
+ <div id="wayke-ev"></div>
13
+ <script type="module">
14
+ import { mount } from 'https://cdn.wayke.se/public-assets/wayke-ev-web/1.0.0/index.js';
15
+
16
+ mount('#wayke-ev', { id: '<vehicle id>' });
17
+ </script>
18
+ ```
19
+
20
+ Two things to know:
21
+
22
+ * **Your domain has to be registered with Wayke.** Until it is, nothing is shown. Ask your contact at Wayke to register it.
23
+ * **Only battery-electric cars have a charging cost.** For any other vehicle nothing is shown, so the snippet can be on every vehicle page.
24
+
25
+ That is all most sites need. The rest of this page covers npm, the options and the details.
26
+
27
+ ## With npm
8
28
 
9
29
  ```bash
10
30
  npm install @wayke-se/ev-web
11
31
  ```
12
32
 
13
- ```js
14
- import WaykeEvWeb from "@wayke-se/ev-web";
15
- import "@wayke-se/ev-web/dist/index.css";
33
+ ```ts
34
+ import { mount } from '@wayke-se/ev-web';
35
+ import '@wayke-se/ev-web/style.css';
16
36
 
17
- const context = new WaykeEvWeb({
18
- url: 'https://api.wayke.se',
19
- id: '4a18e9a4-f7ee-458e-8bb4-9b6b8b778bbf',
20
- });
37
+ const calculator = mount('#wayke-ev', { id: '<vehicle id>' });
38
+
39
+ // When the element goes away:
40
+ calculator.unmount();
21
41
  ```
22
42
 
23
- __Using cdn__
24
- The associated css is injected into head by default once a `new WaykeEvWeb(...)` is called. This can be turned off by adding `disablecssinjection` to the script tag.
43
+ The first argument is a CSS selector or an element. The package is ESM only, has no dependencies and comes with TypeScript types. It needs a browser, so call `mount` on the client.
25
44
 
45
+ <details>
46
+ <summary>In React</summary>
26
47
 
27
- ```html
28
- <script
29
- type="module"
30
- src="https://cdn.wayke.se/public-assets/wayke-ev-web/x.x.x/index.js"
31
- ></script>
32
-
33
- <script>
34
- window.addEventListener("DOMContentLoaded", (_) => {
35
- var context = new WaykeEvWeb({
36
- url: 'https://api.wayke.se',
37
- id: '4a18e9a4-f7ee-458e-8bb4-9b6b8b778bbf',
38
- });
39
- });
40
- </script>
48
+ ```tsx
49
+ import { useEffect, useRef } from 'react';
50
+ import { mount } from '@wayke-se/ev-web';
51
+ import '@wayke-se/ev-web/style.css';
52
+
53
+ export const ChargingCost = ({ id }: { id: string }) => {
54
+ const ref = useRef<HTMLDivElement>(null);
55
+
56
+ useEffect(() => {
57
+ if (!ref.current) return;
58
+ const calculator = mount(ref.current, { id });
59
+ return () => calculator.unmount();
60
+ }, [id]);
61
+
62
+ return <div ref={ref} />;
63
+ };
41
64
  ```
42
65
 
43
- ## Full configuration
66
+ </details>
67
+
68
+ ## Options
69
+
70
+ Either `id` or `data` is required. Everything else is optional.
71
+
72
+ | Option | Default | |
73
+ |--------|---------|---|
74
+ | `id` | | The id of the vehicle's ad at Wayke. The calculator fetches the figures itself. |
75
+ | `data` | | The figures, when your page already has them. See [Passing the figures yourself](#passing-the-figures-yourself). |
76
+ | `environment` | `'production'` | `'test'` uses Wayke's test environment, see [Test environment](#test-environment). |
77
+ | `apiKey` | The origin of the page | Identifies the site instead of its domain. |
78
+ | `marketCode` | The market of the vehicle | `'SE'` or `'NO'`. Sweden counts the distance in mil, Norway in km. |
79
+ | `language` | `sv` in Sweden, `nb` in Norway | `'sv'`, `'nb'` or `'en'`. |
80
+ | `theme` | | Your colors, see [Colors and fonts](#colors-and-fonts). |
81
+ | `hash` | `false` | Adds `#wayke-ev` to the url while the calculator dialog is open, so the browser's back button closes it. Useful on mobile, where the dialog covers the screen. |
82
+ | `onEvent` | | See [Events](#events). |
83
+
84
+ `mount` returns `{ unmount }`. It throws if no element matches the target, or if neither `id` nor `data` is given. It never throws for a vehicle or a network problem: nothing is shown, and [`onEvent`](#events) tells you why.
85
+
86
+ ## Colors and fonts
87
+
88
+ The calculator has no font of its own and inherits the font of your page. Its accents are turquoise until you set your own colors:
44
89
 
45
90
  ```js
46
- new WaykeEvWeb({
47
- url: 'https://api.wayke.se',
48
- marketCode: 'SE',
49
- id: '4a18e9a4-f7ee-458e-8bb4-9b6b8b778bbf',
50
- rootId: 'custom-id',
91
+ mount('#wayke-ev', {
92
+ id: '<vehicle id>',
51
93
  theme: {
52
- primaryColor: '#227a29',
53
- primaryColorForeground: '#b8dbbb',
54
- linkColor: '#0000ff',
94
+ primaryColor: '#227a29', // slider and radio buttons
95
+ primaryColorForeground: '#ffffff', // placed on top of primaryColor
96
+ linkColor: '#19b52f',
55
97
  },
56
- onEvent: (view, event, data) => console.log(view, event, data),
57
98
  });
58
99
  ```
59
100
 
60
- ### Required
61
- * `id` Id of the vehicle from Wayke or a swedish registration number
62
- * `url` Should be one of the following urls below:
101
+ To change more, add your own CSS. All class names start with `waykeev-`, inside `.waykeev-root`, and a class selector of yours always wins over the calculator's own styles. The class names can change between versions, so check your CSS when you update.
63
102
 
64
- | Environment | Url |
65
- | ----------- | --- |
66
- | Test | https://api.wayketech.se |
67
- | Production | https://api.wayke.se |
103
+ ## Events
68
104
 
105
+ `onEvent` is called with one object, and `type` tells the events apart.
69
106
 
70
- ### Optional
107
+ | `type` | Other fields | When |
108
+ |--------|--------------|------|
109
+ | `ready` | `data` | The calculator is on the page. |
110
+ | `unavailable` | `reason` | Nothing is shown: the vehicle has no charging cost. |
111
+ | `error` | `reason` | Nothing is shown: the request failed. |
112
+ | `distance-change` | `distanceKm`, `control` | The visitor changed the yearly distance. In km in both markets. `control` is `'slider'` or `'select'`. |
113
+ | `modal-open`, `modal-close` | | The dialog for an own electricity price. |
114
+ | `price-option-change` | `priceOption` | `'standard'` or `'custom'`. |
115
+ | `custom-price-change` | `pricePerKwh` | The visitor's own price per kWh, in the market's currency: 1.5 for 150 öre. |
71
116
 
72
- - `marketCode` Provide custom element that WaykeValuationWeb should append to. Defaults to `undefined`, possible values are `EN`, `SE` and `NO`. If left undefined then `EN` will be used.
73
- - `rootId` Choose the element, id, that WakeEvWeb should append to, by default it will be appended to the body.
74
- - `hash` Enables hash in route. Will prevent the user from navigating to the previous page when using the back button. It's recommended to enable this option for a better experience in mobile as the modal takes up the whole screen.
75
- - `onEvent` Listen to events. See [Events](#events) below.
76
- - `theme.primaryColor` Override primary color. Defaults to `#00b6d3`
77
- - `theme.primaryColorForeground` Override color placed on top of the primary color. Defaults to `#fff`
78
- - `theme.linkColor` Override color of links. Defaults to `#00b6d3`
117
+ The calculator takes no space until it has figures to show, so your page keeps its layout. When nothing is shown, `reason` is one of:
79
118
 
80
- ### Events
119
+ | `type` | `reason` | Meaning |
120
+ |--------|----------|---------|
121
+ | `unavailable` | `no-charging-data` | The vehicle is not battery-electric, or Wayke lacks the data to estimate its cost. |
122
+ | `unavailable` | `not-found` | No published vehicle has this id in this environment. |
123
+ | `error` | `unauthorized` | The domain of the page, or `apiKey`, is not registered with Wayke in this environment. |
124
+ | `error` | `rate-limited`, `network`, `server` | The request did not succeed. |
81
125
 
82
- | View | Event | data |
83
- | ------|------------------------------- | ------------------------------------------------------------------- |
84
- | MAIN | INITIALIZED | `{ "id": string }` |
85
- | MAIN | INITIAL_REQUEST_PENDING | `{ "id": string }` |
86
- | MAIN | INITIAL_REQUEST_SUCCEEDED | `{ "id": string }` |
87
- | MAIN | INITIAL_REQUEST_FAILED | `{ "id": string, "statusCode": number \| undefined }` |
88
- | MAIN | DISTANCE_SLIDER | `{ "id": string }` |
89
- | MAIN | DISTANCE_SELECT | `{ "id": string }` |
90
- | MAIN | MODAL_OPENED | `{ "id": string }` |
91
- | MODAL | MODAL_CLOSED | `{ "id": string }` |
92
- | MODAL | REQUEST_CUSTOM_PRICE_PENDING | `{ "id": string }` |
93
- | MODAL | REQUEST_CUSTOM_PRICE_SUCCEEDED | `{ "id": string }` |
94
- | MODAL | REQUEST_CUSTOM_PRICE_FAILED | `{ "id": string, "statusCode": number \| undefined }` |
95
- | MODAL | TOGGLE_PRICE | `{ "id": string, "priceOption": 'standardPrice' \| 'customPrice' }` |
96
- | MODAL | CUSTOM_PRICE_CHANGE | `{ "id": string, "customPrice": number }` |
97
- ## CSS specificity
126
+ To show a heading of your own only when there is a calculator, wait for `ready`:
98
127
 
99
- ⚠️ **We can guarantee to not overwrite any of the host site CSS. But it is possible for the host site to overwrite the styling in _Wayke EV_. Read how to prevent such overwrites below.**
128
+ ```js
129
+ mount('#wayke-ev', {
130
+ id: '<vehicle id>',
131
+ onEvent: (event) => {
132
+ if (event.type === 'ready') document.querySelector('#charging-heading').hidden = false;
133
+ },
134
+ });
135
+ ```
136
+
137
+ ## Test environment
138
+
139
+ Set `environment: 'test'` while you develop, to work with Wayke's test vehicles:
100
140
 
101
- All CSS class names are prefixed using the namespace `.waykeev-*`. This will prevent this widget to overwrite any CSS applied to the website on which it is implemented on. However, we can not guarantee other CSS files won't overwrite the CSS in _Wayke EV_. This is due to it is impossible to protect against element selectors.
141
+ ```js
142
+ mount('#wayke-ev', { id: '<vehicle id>', environment: 'test' });
143
+ ```
102
144
 
103
- Reset styles and other necessary CSS targeting elements are selected using a nested selector based of the root element (`.waykeev-root`) placed inside a `:where()` to prevent higher specificity than class name selectors.
145
+ The domain has to be registered in the environment you use, so have `localhost` or your staging domain registered in test. A vehicle id belongs to one environment: an id from production is unknown in test.
104
146
 
105
- > Avoid using element selectors (div, a, span etc.) in you website's CSS. It is a good practice to only using classes as selectors. If you experience strange styling in the _Wayke EV_ widget, this is probably the case.
147
+ ## Passing the figures yourself
106
148
 
107
- ## Custom CSS
149
+ If your page already reads the vehicle from Wayke's GraphQL API, pass the figures as `data`. The calculator then makes no request and is on the page at once.
108
150
 
109
- If you want to custommize any CSS we recommend you to include a separate CSS file with your styling using our selectors namespaced for higher specificity. I.e., `.you-company-name .waykeev-[COMPONENT_NAME] { };`.
151
+ ```ts
152
+ import { mount, type ChargingData } from '@wayke-se/ev-web';
153
+
154
+ const data: ChargingData = {
155
+ energyPer100Km: 16.34, // kWh per 100 km, charging losses included
156
+ costPerKm: 0.198, // at pricePerKwh, in the market's currency
157
+ pricePerKwh: 1.21, // Wayke's template electricity price
158
+ marketCode: 'SE',
159
+ batteryCapacityKwh: 75, // optional
160
+ rangeKm: 514, // optional
161
+ };
162
+
163
+ mount('#wayke-ev', { data });
164
+ ```
110
165
 
111
- The easiest way to add a namespace is to wrap the mounting element (`#custom-id`) with a div having this namespace set following the example below.
166
+ <details>
167
+ <summary>Where the figures are in the GraphQL API</summary>
168
+
169
+ They are attributes of the vehicle. The three `charging*` attributes are `null` for a vehicle without a charging cost.
170
+
171
+ ```graphql
172
+ query ($id: ID!) {
173
+ ads {
174
+ byAdId(ID: $id) {
175
+ iteration {
176
+ item {
177
+ ... on Vehicle {
178
+ attributes {
179
+ chargingEnergyConsumption { floatValue } # energyPer100Km
180
+ chargingCostPerKilometer { floatValue unit } # costPerKm, unit "SEK/km" or "NOK/km"
181
+ chargingPricePerKwh { floatValue } # pricePerKwh
182
+ batteryCapacityNet { intValue } # batteryCapacityKwh
183
+ electricRangeWltp { minvalue } # rangeKm
184
+ }
185
+ }
186
+ }
187
+ }
188
+ }
189
+ }
190
+ }
191
+ ```
192
+
193
+ </details>
194
+
195
+ ## How the cost is calculated
196
+
197
+ ```
198
+ cost per month = energy per km × price per kWh × distance per year / 12
199
+ ```
200
+
201
+ * **Energy per km** comes from Wayke: the energy drawn from the socket, charging losses included, based on the car's WLTP figures.
202
+ * **Price per kWh** is Wayke's template price for the market, with energy tax and VAT included. The grid fee is not included. The visitor can enter their own price instead, which is calculated in the browser without a request.
203
+ * **Distance per year** is chosen by the visitor: 500 to 5 000 mil in Sweden, 5 000 to 50 000 km in Norway.
204
+
205
+ ## CDN details
206
+
207
+ Every release is published to `https://cdn.wayke.se/public-assets/wayke-ev-web/<version>/index.js`. Use an exact version: its files never change, so your site only changes when you update the version in the snippet. The latest version is shown on [npm](https://www.npmjs.com/package/@wayke-se/ev-web).
208
+
209
+ The script adds its stylesheet, `index.css` in the same folder, to the top of `<head>`, so the styles of your site override it. To load the stylesheet yourself, add a `<link>` to it with the same URL, and the script does not add it again.
210
+
211
+ <details>
212
+ <summary>With a script tag instead of import</summary>
213
+
214
+ The script sets `window.WaykeEv` when it has loaded. Module scripts run after the page has been read, so wait for `DOMContentLoaded`. If the snippet is added after the page has loaded, for example by a tag manager, use the import in [Quick start](#quick-start) instead.
112
215
 
113
216
  ```html
114
- <div class="your-company-name">
115
- <div id="custom-id"></div>
116
- </div>
217
+ <div id="wayke-ev"></div>
218
+ <script type="module" src="https://cdn.wayke.se/public-assets/wayke-ev-web/1.0.0/index.js"></script>
219
+ <script>
220
+ window.addEventListener('DOMContentLoaded', () => {
221
+ WaykeEv.mount('#wayke-ev', { id: '<vehicle id>' });
222
+ });
223
+ </script>
224
+ ```
225
+
226
+ Add `disablecssinjection` to the script tag to not load the stylesheet at all. The calculator is then unstyled.
227
+
228
+ </details>
229
+
230
+ ## Migrating from 0.x to 1.0
231
+
232
+ 1.0 is a rewrite. The calculator looks the same, is a fifth of the size, and no longer makes a request for an own electricity price.
233
+
234
+ ```js
235
+ // 0.x
236
+ import WaykeEvWeb from '@wayke-se/ev-web';
237
+ import '@wayke-se/ev-web/dist/index.css';
238
+
239
+ new WaykeEvWeb({
240
+ url: 'https://api.wayke.se',
241
+ id: '4a18e9a4-f7ee-458e-8bb4-9b6b8b778bbf',
242
+ rootId: 'custom-id',
243
+ marketCode: 'SE',
244
+ });
245
+
246
+ // 1.0
247
+ import { mount } from '@wayke-se/ev-web';
248
+ import '@wayke-se/ev-web/style.css';
249
+
250
+ mount('#custom-id', { id: '4a18e9a4-f7ee-458e-8bb4-9b6b8b778bbf' });
117
251
  ```
118
252
 
119
- > **Important:** We can not guarantee our class names will stay the same or behave the same as they do at a specific time. Please understand that you may have to change the CSS continuously if you decide to add custom styling.
253
+ | 0.x | 1.0 |
254
+ |-----|-----|
255
+ | `new WaykeEvWeb(options)` | `mount(target, options)` |
256
+ | `@wayke-se/ev-web/dist/index.css` | `@wayke-se/ev-web/style.css` |
257
+ | `window.WaykeEvWeb` (CDN) | `window.WaykeEv.mount`, or import `mount` from the script |
258
+ | `url: 'https://api.wayke.se'` | Remove it. Production is the default. |
259
+ | `url: 'https://api.wayketech.se'` | `environment: 'test'` |
260
+ | `rootId: 'custom-id'` | The first argument, `'#custom-id'`. It is required: the calculator is no longer added to `<body>` without it. |
261
+ | `marketCode` | Optional. The market follows the vehicle. |
262
+ | `theme`, `hash` | Unchanged. |
263
+ | `onEvent: (view, event, data) => ...` | `onEvent: (event) => ...`, with new names, see below. |
264
+
265
+ Events:
266
+
267
+ | 0.x | 1.0 |
268
+ |-----|-----|
269
+ | `INITIAL_REQUEST_SUCCEEDED` | `ready` |
270
+ | `INITIAL_REQUEST_FAILED` | `unavailable` or `error`, with a `reason` |
271
+ | `DISTANCE_SLIDER`, `DISTANCE_SELECT` | `distance-change` with `control` |
272
+ | `MODAL_OPENED`, `MODAL_CLOSED` | `modal-open`, `modal-close` |
273
+ | `TOGGLE_PRICE` | `price-option-change`, with `'standard'` or `'custom'` (was `'standardPrice'` or `'customPrice'`) |
274
+ | `CUSTOM_PRICE_CHANGE` | `custom-price-change`, with `pricePerKwh` in kronor (`customPrice` was in öre) |
275
+ | `INITIALIZED`, `INITIAL_REQUEST_PENDING`, `REQUEST_CUSTOM_PRICE_*` | Removed |
276
+
277
+ Also different:
278
+
279
+ * **A registration number is no longer accepted as `id`.** Use the id of the vehicle's ad at Wayke.
280
+ * **The fonts are gone.** 0.x came with Wayke's and Drive's fonts, 1.0 uses the font of your page.
281
+ * **More cars get a cost.** 0.x showed nothing for a car without a battery capacity at Wayke. 1.0 then uses the car's WLTP consumption.
282
+ * **The package is ESM only.** `require('@wayke-se/ev-web')` no longer works.
283
+ * **If nothing is shown after the upgrade**, `onEvent` gets `error` with the reason `unauthorized` when the domain is not registered. Ask your contact at Wayke to check the registration.
284
+
285
+ ## Development
286
+
287
+ ```bash
288
+ npm install
289
+ npm start # the development page on http://localhost:5000, against Wayke's test environment
290
+ npm test # lint, types, stylelint and unit tests
291
+ npm run build # dist/ for npm and dist-cdn/ for the CDN
292
+ ```
120
293
 
121
- ## Headings
294
+ The origin of the development page has to be registered in Wayke's test environment. `PORT=5050 npm start` uses another port, and `WAYKE_API_KEY=<key> npm start` identifies the page with a key instead of its origin.
122
295
 
123
- To prevent multiple `h1` on your website, _Wayke EV_ does not include a `h1` as root heading level. Instead the headings starts from h2.
296
+ A push to `main` or `test` releases the version in `package.json` to the CDN and npm. A pre-release version, such as `1.1.0-beta.1`, gets the npm tag `next`, and so does everything released from `test`. Only a plain version on `main` becomes `latest`. A version that is already published is refused, so bump the version first. The release log prints the CDN url the files are served from.
package/dist/api.d.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { ChargingData, Environment, ErrorReason, UnavailableReason } from './types.js';
2
+ interface AdNode {
3
+ id: string;
4
+ iteration?: {
5
+ item?: {
6
+ attributes?: {
7
+ chargingEnergyConsumption?: {
8
+ floatValue?: number | null;
9
+ } | null;
10
+ chargingCostPerKilometer?: {
11
+ floatValue?: number | null;
12
+ unit?: string | null;
13
+ } | null;
14
+ chargingPricePerKwh?: {
15
+ floatValue?: number | null;
16
+ } | null;
17
+ batteryCapacityNet?: {
18
+ intValue?: number | null;
19
+ } | null;
20
+ batteryCapacityGross?: {
21
+ intValue?: number | null;
22
+ } | null;
23
+ electricRangeWltp?: {
24
+ minvalue?: number | null;
25
+ } | null;
26
+ } | null;
27
+ } | null;
28
+ } | null;
29
+ }
30
+ export declare class ApiError extends Error {
31
+ readonly reason: ErrorReason;
32
+ constructor(reason: ErrorReason);
33
+ }
34
+ export type ChargingDataResult = {
35
+ status: 'ok';
36
+ data: ChargingData;
37
+ } | {
38
+ status: 'unavailable';
39
+ reason: UnavailableReason;
40
+ };
41
+ /** Exported for the tests. */
42
+ export declare const toChargingData: (ad: AdNode | null | undefined) => ChargingDataResult;
43
+ interface FetchOptions {
44
+ id: string;
45
+ environment: Environment;
46
+ apiKey?: string;
47
+ signal?: AbortSignal;
48
+ }
49
+ export declare const fetchChargingData: ({ id, environment, apiKey, signal, }: FetchOptions) => Promise<ChargingDataResult>;
50
+ export {};
@@ -0,0 +1,28 @@
1
+ import type { ChargingData, MarketCode } from './types.js';
2
+ export declare const DEFAULT_DISTANCE_KM = 15000;
3
+ export interface DistanceScale {
4
+ /** Sweden counts in Scandinavian miles (1 mil = 10 km), Norway in km. */
5
+ unit: 'mil' | 'km';
6
+ kmPerUnit: number;
7
+ /** In the market's unit. */
8
+ options: number[];
9
+ min: number;
10
+ max: number;
11
+ step: number;
12
+ }
13
+ export declare const distanceScale: (marketCode: MarketCode) => DistanceScale;
14
+ /** The price in whole öre/øre, which is what the visitor sees and types. */
15
+ export declare const toCents: (pricePerKwh: number) => number;
16
+ /**
17
+ * Cost per km at the visitor's own price per kWh. Undefined for a missing or
18
+ * invalid price.
19
+ *
20
+ * At the template price it is the template cost itself: Wayke computes that figure
21
+ * from an unrounded price, so energy × the rounded price would land a krona or two
22
+ * off per month and the two price options would disagree before anything is typed.
23
+ */
24
+ export declare const costPerKmAtPrice: (data: ChargingData, pricePerKwh: number | undefined) => number | undefined;
25
+ /** Undefined when there is no cost per km. A cost that rounds to 0 is still a cost. */
26
+ export declare const monthlyCost: (costPerKm: number | undefined, distanceKmPerYear: number) => number | undefined;
27
+ /** True when the figures are enough to show a calculator. */
28
+ export declare const isChargingData: (data: Partial<ChargingData> | null | undefined) => data is ChargingData;
package/dist/dom.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ type Attributes = Record<string, string | number | boolean | undefined>;
2
+ type Child = Node | string | false | null | undefined;
3
+ export declare const h: <K extends keyof HTMLElementTagNameMap>(tag: K, attributes?: Attributes, ...children: Child[]) => HTMLElementTagNameMap[K];
4
+ /** A decorative icon: one path in a 16 × 16 viewBox. */
5
+ export declare const icon: (path: string) => SVGSVGElement;
6
+ export declare const ICONS: {
7
+ bulb: string;
8
+ close: string;
9
+ help: string;
10
+ helpClose: string;
11
+ };
12
+ /** The brand symbol shown next to "Powered by". */
13
+ export declare const brandSymbol: (brand: string) => SVGSVGElement;
14
+ export {};
package/dist/i18n.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ import type { Language, MarketCode } from './types.js';
2
+ declare const sv: {
3
+ title: string;
4
+ description: string;
5
+ modalDescription: string;
6
+ yearlyDistance: string;
7
+ adjustToYourPrice: string;
8
+ openCalculator: string;
9
+ closeModal: string;
10
+ costPerMonthUnit: string;
11
+ pricePerKwhUnit: string;
12
+ energyUnit: string;
13
+ unknownCost: string;
14
+ disclaimerStandard: string;
15
+ disclaimerCustom: string;
16
+ whichPrice: string;
17
+ standardPrice: string;
18
+ customPrice: string;
19
+ yourPrice: string;
20
+ yourPriceHelp: string;
21
+ showHelp: string;
22
+ hideHelp: string;
23
+ batteryCapacity: string;
24
+ range: string;
25
+ energyUse: string;
26
+ statedDistance: string;
27
+ standardPriceRow: string;
28
+ gridFee: string;
29
+ gridFeeValue: string;
30
+ poweredBy: string;
31
+ };
32
+ type Messages = typeof sv;
33
+ export type MessageKey = keyof Messages;
34
+ export interface Translator {
35
+ (key: MessageKey, values?: Record<string, string>): string;
36
+ /** Formats a number the way the language writes it, e.g. "1 500" and "20,4". */
37
+ number: (value: number, maximumFractionDigits?: number) => string;
38
+ }
39
+ export declare const defaultLanguage: (marketCode: MarketCode) => Language;
40
+ export declare const createTranslator: (language: Language) => Translator;
41
+ /** The brand behind the figures: Wayke in Sweden, Drive in Norway. */
42
+ export declare const brandOf: (marketCode: MarketCode) => {
43
+ name: string;
44
+ url: string;
45
+ };
46
+ export {};