@schalkneethling/miyagi-core 4.4.0 → 4.4.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.
@@ -14,17 +14,17 @@ import log from "../logger.js";
14
14
  * @returns {object} the mock data of the given component
15
15
  */
16
16
  function getDataForComponent(component) {
17
- const { fileContents } = global.state;
18
- const { name, extension } = global.config.files.mocks;
17
+ const { fileContents } = global.state;
18
+ const { name, extension } = global.config.files.mocks;
19
19
 
20
- const defaultPath = path.join(
21
- component.paths.dir.full,
22
- `${name}.${extension[0]}`,
23
- );
20
+ const defaultPath = path.join(
21
+ component.paths.dir.full,
22
+ `${name}.${extension[0]}`,
23
+ );
24
24
 
25
- const jsPath = path.join(component.paths.dir.full, `${name}.${extension[1]}`);
25
+ const jsPath = path.join(component.paths.dir.full, `${name}.${extension[1]}`);
26
26
 
27
- return fileContents[defaultPath] || fileContents[jsPath];
27
+ return fileContents[defaultPath] || fileContents[jsPath];
28
28
  }
29
29
 
30
30
  /**
@@ -33,7 +33,7 @@ function getDataForComponent(component) {
33
33
  * @returns {boolean} is true of the requested variation is in the given mock data
34
34
  */
35
35
  function checkIfDataIncludesVariation(data, variation) {
36
- return data?.$variants?.find((variant) => variant.$name === variation);
36
+ return data?.$variants?.find((variant) => variant.$name === variation);
37
37
  }
38
38
 
39
39
  /**
@@ -42,209 +42,209 @@ function checkIfDataIncludesVariation(data, variation) {
42
42
  * @returns {boolean} is true if the requested variation exists in the mock data of the given component
43
43
  */
44
44
  function checkIfRequestedVariationIsValid(component, variation) {
45
- const data = getDataForComponent(component);
45
+ const data = getDataForComponent(component);
46
46
 
47
- if (
48
- data &&
49
- (variation === data.$name || variation === config.defaultVariationName) &&
50
- !data.$hidden
51
- ) {
52
- return true;
53
- }
47
+ if (
48
+ data &&
49
+ (variation === data.$name || variation === config.defaultVariationName) &&
50
+ !data.$hidden
51
+ ) {
52
+ return true;
53
+ }
54
54
 
55
- if (!data && variation === config.defaultVariationName) {
56
- return true;
57
- }
55
+ if (!data && variation === config.defaultVariationName) {
56
+ return true;
57
+ }
58
58
 
59
- return checkIfDataIncludesVariation(data, variation);
59
+ return checkIfDataIncludesVariation(data, variation);
60
60
  }
61
61
 
62
62
  /**
63
63
  * @returns {void}
64
64
  */
65
65
  export default function Router() {
66
- global.app.get("/design-tokens/colors", async (req, res) => {
67
- return render.renderMainDesignTokens({
68
- res,
69
- cookies: req.cookies,
70
- type: "colors",
71
- });
72
- });
73
-
74
- global.app.get("/iframe/design-tokens/colors", async (req, res) => {
75
- return render.iframe.designTokens.colors({ res, cookies: req.cookies });
76
- });
77
-
78
- global.app.get("/design-tokens/sizes", async (req, res) => {
79
- return render.renderMainDesignTokens({
80
- res,
81
- cookies: req.cookies,
82
- type: "sizes",
83
- });
84
- });
85
-
86
- global.app.get("/iframe/design-tokens/sizes", async (req, res) => {
87
- return render.iframe.designTokens.sizes({ res, cookies: req.cookies });
88
- });
89
-
90
- global.app.get("/design-tokens/typography", async (req, res) => {
91
- return render.renderMainDesignTokens({
92
- res,
93
- cookies: req.cookies,
94
- type: "typography",
95
- });
96
- });
97
-
98
- global.app.get("/iframe/design-tokens/typography", async (req, res) => {
99
- return render.iframe.designTokens.typography({
100
- res,
101
- cookies: req.cookies,
102
- });
103
- });
104
-
105
- global.app.get("/show", async (req, res) => {
106
- const { file, variation } = req.query;
107
-
108
- if (!file) {
109
- return res.redirect(302, "/");
110
- }
111
-
112
- if (file === "all") {
113
- return await render.renderMainIndex({ res, cookies: req.cookies });
114
- }
115
-
116
- const routesEntry = global.state.routes.find(
117
- (route) => route.paths.dir.short === file,
118
- );
119
-
120
- if (!routesEntry) {
121
- return res.redirect(302, "/");
122
- }
123
-
124
- switch (routesEntry.type) {
125
- case "components": {
126
- if (!routesEntry.paths.tpl) {
127
- return await render.renderMainComponentDocs({
128
- res,
129
- component: routesEntry,
130
- cookies: req.cookies,
131
- });
132
- }
133
-
134
- return await render.renderMainComponent(
135
- checkIfRequestedVariationIsValid(routesEntry, variation)
136
- ? {
137
- res,
138
- component: routesEntry,
139
- cookies: req.cookies,
140
- variation,
141
- }
142
- : {
143
- res,
144
- component: routesEntry,
145
- cookies: req.cookies,
146
- },
147
- );
148
- }
149
-
150
- case "docs": {
151
- return await render.renderMainDocs({
152
- res,
153
- doc: routesEntry,
154
- cookies: req.cookies,
155
- });
156
- }
157
-
158
- default:
159
- return res.redirect(302, "/");
160
- }
161
- });
162
-
163
- global.app.get("/component", async (req, res) => {
164
- const { file, variation, embedded } = req.query;
165
-
166
- if (!file) {
167
- return res.redirect(302, global.config.indexPath.default);
168
- }
169
-
170
- if (file === "all") {
171
- return await render.renderIframeIndex({ res, cookies: req.cookies });
172
- }
173
-
174
- const routesEntry = global.state.routes.find(
175
- (route) => route.paths.dir.short === file,
176
- );
177
-
178
- if (!routesEntry) {
179
- res.redirect(302, global.config.indexPath.default);
180
- }
181
-
182
- switch (routesEntry.type) {
183
- case "components": {
184
- if (!routesEntry.paths.tpl) {
185
- return await render.renderIframeComponentDocs({
186
- res,
187
- component: routesEntry,
188
- cookies: req.cookies,
189
- });
190
- }
191
-
192
- if (
193
- !variation ||
194
- !checkIfRequestedVariationIsValid(routesEntry, variation)
195
- ) {
196
- return await render.renderIframeComponent({
197
- res,
198
- component: routesEntry,
199
- cookies: req.cookies,
200
- });
201
- }
202
-
203
- const data = await getVariationData(routesEntry, decodeURI(variation));
204
-
205
- if (embedded) {
206
- return await render.renderIframeVariation({
207
- res,
208
- component: routesEntry,
209
- variation,
210
- cookies: req.cookies,
211
- data,
212
- });
213
- }
214
-
215
- if (data?.messages?.length) {
216
- for (const { type, text, verbose } of data.messages) {
217
- log(type, text, verbose);
218
- }
219
- }
220
-
221
- return await render.renderIframeVariationStandalone({
222
- res,
223
- component: routesEntry,
224
- componentData: data?.resolved ?? {},
225
- componentDeclaredAssets: data?.$assets || null,
226
- cookies: req.cookies,
227
- });
228
- }
229
-
230
- case "docs": {
231
- return await render.renderIframeDocs({
232
- res,
233
- doc: routesEntry,
234
- cookies: req.cookies,
235
- });
236
- }
237
-
238
- default:
239
- res.redirect(302, global.config.indexPath.default);
240
- }
241
- });
242
-
243
- global.app.get("/", async (req, res) => {
244
- await render.renderMainIndex({ res, cookies: req.cookies });
245
- });
246
-
247
- global.app.all("*splat", async (req, res) => {
248
- res.sendStatus(404);
249
- });
66
+ global.app.get("/design-tokens/colors", async (req, res) => {
67
+ return render.renderMainDesignTokens({
68
+ res,
69
+ cookies: req.cookies,
70
+ type: "colors",
71
+ });
72
+ });
73
+
74
+ global.app.get("/iframe/design-tokens/colors", async (req, res) => {
75
+ return render.iframe.designTokens.colors({ res, cookies: req.cookies });
76
+ });
77
+
78
+ global.app.get("/design-tokens/sizes", async (req, res) => {
79
+ return render.renderMainDesignTokens({
80
+ res,
81
+ cookies: req.cookies,
82
+ type: "sizes",
83
+ });
84
+ });
85
+
86
+ global.app.get("/iframe/design-tokens/sizes", async (req, res) => {
87
+ return render.iframe.designTokens.sizes({ res, cookies: req.cookies });
88
+ });
89
+
90
+ global.app.get("/design-tokens/typography", async (req, res) => {
91
+ return render.renderMainDesignTokens({
92
+ res,
93
+ cookies: req.cookies,
94
+ type: "typography",
95
+ });
96
+ });
97
+
98
+ global.app.get("/iframe/design-tokens/typography", async (req, res) => {
99
+ return render.iframe.designTokens.typography({
100
+ res,
101
+ cookies: req.cookies,
102
+ });
103
+ });
104
+
105
+ global.app.get("/show", async (req, res) => {
106
+ const { file, variation } = req.query;
107
+
108
+ if (!file) {
109
+ return res.redirect(302, "/");
110
+ }
111
+
112
+ if (file === "all") {
113
+ return await render.renderMainIndex({ res, cookies: req.cookies });
114
+ }
115
+
116
+ const routesEntry = global.state.routes.find(
117
+ (route) => route.paths.dir.short === file,
118
+ );
119
+
120
+ if (!routesEntry) {
121
+ return res.redirect(302, "/");
122
+ }
123
+
124
+ switch (routesEntry.type) {
125
+ case "components": {
126
+ if (!routesEntry.paths.tpl) {
127
+ return await render.renderMainComponentDocs({
128
+ res,
129
+ component: routesEntry,
130
+ cookies: req.cookies,
131
+ });
132
+ }
133
+
134
+ return await render.renderMainComponent(
135
+ checkIfRequestedVariationIsValid(routesEntry, variation)
136
+ ? {
137
+ res,
138
+ component: routesEntry,
139
+ cookies: req.cookies,
140
+ variation,
141
+ }
142
+ : {
143
+ res,
144
+ component: routesEntry,
145
+ cookies: req.cookies,
146
+ },
147
+ );
148
+ }
149
+
150
+ case "docs": {
151
+ return await render.renderMainDocs({
152
+ res,
153
+ doc: routesEntry,
154
+ cookies: req.cookies,
155
+ });
156
+ }
157
+
158
+ default:
159
+ return res.redirect(302, "/");
160
+ }
161
+ });
162
+
163
+ global.app.get("/component", async (req, res) => {
164
+ const { file, variation, embedded } = req.query;
165
+
166
+ if (!file) {
167
+ return res.redirect(302, global.config.indexPath.default);
168
+ }
169
+
170
+ if (file === "all") {
171
+ return await render.renderIframeIndex({ res, cookies: req.cookies });
172
+ }
173
+
174
+ const routesEntry = global.state.routes.find(
175
+ (route) => route.paths.dir.short === file,
176
+ );
177
+
178
+ if (!routesEntry) {
179
+ return res.redirect(302, global.config.indexPath.default);
180
+ }
181
+
182
+ switch (routesEntry.type) {
183
+ case "components": {
184
+ if (!routesEntry.paths.tpl) {
185
+ return await render.renderIframeComponentDocs({
186
+ res,
187
+ component: routesEntry,
188
+ cookies: req.cookies,
189
+ });
190
+ }
191
+
192
+ if (
193
+ !variation ||
194
+ !checkIfRequestedVariationIsValid(routesEntry, variation)
195
+ ) {
196
+ return await render.renderIframeComponent({
197
+ res,
198
+ component: routesEntry,
199
+ cookies: req.cookies,
200
+ });
201
+ }
202
+
203
+ const data = await getVariationData(routesEntry, decodeURI(variation));
204
+
205
+ if (embedded) {
206
+ return await render.renderIframeVariation({
207
+ res,
208
+ component: routesEntry,
209
+ variation,
210
+ cookies: req.cookies,
211
+ data,
212
+ });
213
+ }
214
+
215
+ if (data?.messages?.length) {
216
+ for (const { type, text, verbose } of data.messages) {
217
+ log(type, text, verbose);
218
+ }
219
+ }
220
+
221
+ return await render.renderIframeVariationStandalone({
222
+ res,
223
+ component: routesEntry,
224
+ componentData: data?.resolved ?? {},
225
+ componentDeclaredAssets: data?.$assets || null,
226
+ cookies: req.cookies,
227
+ });
228
+ }
229
+
230
+ case "docs": {
231
+ return await render.renderIframeDocs({
232
+ res,
233
+ doc: routesEntry,
234
+ cookies: req.cookies,
235
+ });
236
+ }
237
+
238
+ default:
239
+ res.redirect(302, global.config.indexPath.default);
240
+ }
241
+ });
242
+
243
+ global.app.get("/", async (req, res) => {
244
+ await render.renderMainIndex({ res, cookies: req.cookies });
245
+ });
246
+
247
+ global.app.all("*splat", async (req, res) => {
248
+ res.sendStatus(404);
249
+ });
250
250
  }