@8ms/helpers 1.2.4 → 1.2.5

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.2.4",
4
+ "version": "1.2.5",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"
@@ -0,0 +1,12 @@
1
+ type SaveUrlContents = {
2
+ fileName: string;
3
+ path?: string;
4
+ url: string;
5
+ };
6
+ /**
7
+ * Downloads a given URL's contents to a local file.
8
+ *
9
+ * Requires fs-extra
10
+ */
11
+ declare const saveUrlContents: ({ fileName, path, url }: SaveUrlContents) => Promise<unknown>;
12
+ export default saveUrlContents;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const axios_1 = __importDefault(require("axios"));
7
+ /**
8
+ * Downloads a given URL's contents to a local file.
9
+ *
10
+ * Requires fs-extra
11
+ */
12
+ const saveUrlContents = ({ fileName, path, url }) => {
13
+ const fs = require('fs-extra');
14
+ let finalPath = path ? path : '/tmp';
15
+ // Must begin with slash
16
+ if ('/' !== finalPath[0]) {
17
+ finalPath = '/' + finalPath;
18
+ }
19
+ // Must end without slash
20
+ if ('/' === finalPath[finalPath.length - 1]) {
21
+ finalPath = finalPath.substring(0, finalPath.length - 2);
22
+ }
23
+ return new Promise(async (resolve, reject) => {
24
+ (0, axios_1.default)({
25
+ method: "get",
26
+ url,
27
+ responseType: "stream"
28
+ })
29
+ .then(function (response) {
30
+ response.data.pipe(fs.createWriteStream(`${finalPath}/${fileName}`));
31
+ resolve(true);
32
+ });
33
+ });
34
+ };
35
+ exports.default = saveUrlContents;