Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.

Commit 6827e8a

Browse files
committed
Handles unauth errors from Streamline
1 parent 90d7d2f commit 6827e8a

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

.npmignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.idea
2+
3+
# dependencies
4+
/node_modules
5+
6+
7+
# misc
8+
.DS_Store
9+
10+
.env
11+
12+
tmp
13+
*.log*
14+
15+
.eslintcache
16+
17+
.babelrc
18+
.editorconfig
19+
.eslintignore
20+
.eslintrc.js
21+
.prettierrc.js
22+
rollup.config.js
23+
tsconfig.json
24+
25+
docs/
26+
src/

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Universal NPM package for Streamline icons and illustrations
2+
3+
This is a small library which downloads Streamline assets you have access to into your local folder so that they can be used in your Javascript project.
4+
5+
## How to install
6+
7+
`yarn add XXX`
8+
9+
## How to use
10+
11+
1. Ensure that you have an active Streamline icons or illustrations subscription.
12+
2. In your project folder create a special `streamlinehq.json` settings file. Fill it with two keys:
13+
- `families`: an array of strings with names of Streamline icons or illustrations families you want to include in your project.
14+
- `secret`: XXX
15+
16+
Example:
17+
```json
18+
{
19+
"families": ["streamline-regular", "streamline-light"],
20+
"secret": "MY.AUTHTOKEN"
21+
}
22+
```
23+
3. Install your packages, eg with `yarn`
24+
25+
The package will run a script during installation which will fetch requested icons in a form of SVG files and put them in the package's `images` folder. After this you will be able to import those images as usual in your project.
26+
27+
If you have any questions please open an issue.

index.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ async function fetchFamilies(secret, families) {
1313
// https
1414
http
1515
.get(
16-
// TODO: change for the real endpoint when it is ready
16+
// TODO: change for the real endpoint when package is ready
1717
// `https://api.staging.streamlineicons.com/v3/icons/${family}`,
18-
`http://localhost:8080/v3/icons/${family}`,
18+
`http://localhost:8080/v3/icons/${family}?withSVG=true`,
1919
{
2020
headers: {
2121
Authorization: `Bearer ${secret}`,
@@ -31,7 +31,7 @@ async function fetchFamilies(secret, families) {
3131

3232
// The whole response has been received. Print out the result.
3333
resp.on('end', () => {
34-
resolve(JSON.parse(data))
34+
resolve({ ...JSON.parse(data), familyName: family, statusCode: resp.statusCode })
3535
})
3636
}
3737
)
@@ -69,11 +69,22 @@ async function installStreamlineAssets() {
6969
const fetchedFamiliesResponses = await fetchFamilies(streamlineConfiguration.secret, streamlineConfiguration.families);
7070

7171
fetchedFamiliesResponses.forEach((familyResponse) => {
72-
familyResponse.data.forEach(async (icon) => {
73-
const folderPath = `${__dirname}/images/${icon.family.slug}`
74-
await mkdir(folderPath, {recursive: true})
75-
await writeFile(`${folderPath}/${icon.slug}.svg`, icon.svg)
76-
})
72+
if (familyResponse.success) {
73+
familyResponse.data.forEach(async (icon) => {
74+
if (!icon.svg) {
75+
throw new Error(`No SVG data is present, please report an issue to Streamline team about icon ${icon.slug} of family ${icon.family.slug}`)
76+
}
77+
const folderPath = `${__dirname}/images/${icon.family.slug}`
78+
await mkdir(folderPath, {recursive: true})
79+
await writeFile(`${folderPath}/${icon.slug}.svg`, icon.svg)
80+
})
81+
} else {
82+
let errorMessage = `Got error "${familyResponse.error}" for family ${familyResponse.familyName}.`
83+
if (familyResponse.statusCode === 401) {
84+
errorMessage += ` Error code is 401 which means it's most likely related to the auth token which was provided. Please double check its value by following the instructions in the project's README file.`
85+
}
86+
throw new Error(errorMessage)
87+
}
7788
})
7889

7990
console.debug("Finished installation");

0 commit comments

Comments
 (0)