Skip to content

Commit f61960b

Browse files
committed
Refactor fetch to use Promise.all.
1 parent 79df04e commit f61960b

File tree

1 file changed

+46
-41
lines changed

1 file changed

+46
-41
lines changed

components/CredentialDetailsViews.vue

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@
134134
/*!
135135
* Copyright (c) 2015-2024 Digital Bazaar, Inc. All rights reserved.
136136
*/
137-
import {onBeforeMount, onMounted, reactive, ref} from 'vue';
137+
import {onMounted, reactive, ref} from 'vue';
138138
import {date} from 'quasar';
139+
import {httpClient} from '@digitalbazaar/http-client';
139140
import Mustache from 'mustache';
140141
141142
const {formatDate} = date;
@@ -181,6 +182,7 @@ export default {
181182
} else if(showDisplays.value) {
182183
tab.value = 'displays';
183184
}
185+
getDisplaysFromRenderMethod();
184186
});
185187
186188
// Constants
@@ -197,37 +199,28 @@ export default {
197199
backgroundColor: 'gray',
198200
};
199201
200-
// Fetch style, overrides, & highlights before component mounts
201-
onBeforeMount(() => {
202-
getDisplaysFromRenderMethod();
203-
});
204-
205202
// Extract and parse images from credential's render method property
206203
async function getDisplaysFromRenderMethod() {
207204
if(props.credential?.renderMethod?.length) {
208-
props.credential.renderMethod.forEach(async rm => {
209-
if(supportedRenderMethods.includes(rm.type)) {
210-
if(rm.type === 'SvgRenderingTemplate2023') {
211-
useRenderTemplate2023(rm.id);
212-
} else if(rm.type === 'SvgRenderingTemplate2024') {
213-
const {template, url} = rm;
214-
const values = props.credential;
215-
await useRenderTemplate2024(template, url, values);
205+
const imageValues = await Promise.all(
206+
props.credential.renderMethod.map(async rm => {
207+
let imageValue;
208+
if(supportedRenderMethods.includes(rm.type)) {
209+
if(rm.type === 'SvgRenderingTemplate2023') {
210+
imageValue = useRenderTemplate2023(rm.id);
211+
} else if(rm.type === 'SvgRenderingTemplate2024') {
212+
const {template, url} = rm;
213+
const values = props.credential;
214+
imageValue = await useRenderTemplate2024(template, url, values);
215+
}
216216
}
217-
}
218-
});
217+
return imageValue;
218+
})
219+
);
220+
imageValues.filter(Boolean).forEach(v => credentialImages.push(v));
219221
}
220222
}
221223
222-
/**
223-
* Uses id for src value in image.
224-
*
225-
* @param {string} srcValue - Data URI.
226-
*/
227-
function useRenderTemplate2023(srcValue) {
228-
credentialImages.push(srcValue);
229-
}
230-
231224
/*
232225
* Functions used to format Mustache template values
233226
* See: https://github.com/janl/mustache.js#functions
@@ -242,34 +235,46 @@ export default {
242235
}
243236
};
244237
238+
/**
239+
* Uses id for src value in image.
240+
*
241+
* @param {string} srcValue - Data URI.
242+
* @returns {string} Src value.
243+
*/
244+
function useRenderTemplate2023(srcValue) {
245+
return srcValue;
246+
}
247+
245248
/**
246249
* Load svg from url or template then hydrate with credentialSubject values.
247250
*
248251
* @param {string} template - Svg.
249252
* @param {string} url - Url.
250253
* @param {object} values - Credential.credentialSubject.
254+
* @returns {string} Src value.
251255
*/
252256
async function useRenderTemplate2024(template, url, values) {
253-
// Example credential renderMethod property:
254-
// "renderMethod": [
255-
// {
256-
// "name": "Landscape",
257-
// "mediaQuery": "@media (orientation: landscape)",
258-
// "type": "SvgRenderingTemplate2024",
259-
// "template": "",
260-
// "url": "https://credentialTemplates.dev/example.svg",
261-
// "mediaType": "image/svg+xml",
262-
// }
263-
// ]
264-
//
257+
/*
258+
* Example credential renderMethod property:
259+
* "renderMethod": [
260+
* {
261+
* "name": "Landscape",
262+
* "mediaQuery": "@media (orientation: landscape)",
263+
* "type": "SvgRenderingTemplate2024",
264+
* "template": "",
265+
* "url": "https://credentialTemplates.dev/example.svg",
266+
* "mediaType": "image/svg+xml",
267+
* }
268+
* ]
269+
*/
265270
if(!template || url) {
266-
const resp = await fetch(url);
267-
template = await resp.text();
271+
const headers = {headers: {accept: 'application/json'}};
272+
const {data} = await httpClient.get(url, {headers});
273+
template = await data.text();
268274
}
269-
270275
const rv = Mustache.render(template, {...values, ...formattingFunctions});
271276
const image = `data:image/svg+xml;base64,${btoa(rv)}`;
272-
credentialImages.push(image);
277+
return image;
273278
}
274279
275280
return {

0 commit comments

Comments
 (0)