diff --git a/package.json b/package.json index 51a07c0..c7e81a0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nevermined-io/payments", - "version": "0.4.1", + "version": "0.4.2-rc0", "description": "Typescript SDK to interact with the Nevermined Payments Protocol", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -35,5 +35,8 @@ "tslib": "^2.6.2", "typedoc": "0.25.13", "typescript": "^5.3.3" + }, + "dependencies": { + "js-file-download": "^0.4.12" } } diff --git a/src/payments.ts b/src/payments.ts index 602d876..f5e164b 100644 --- a/src/payments.ts +++ b/src/payments.ts @@ -1,4 +1,8 @@ import { EnvironmentInfo, EnvironmentName, Environments } from './environments' +import * as path from 'path' +import fileDownload from 'js-file-download' +import fs from 'fs' + /** * Options to initialize the Payments class. */ @@ -684,7 +688,7 @@ export class Payments { * @param nvmApiKey - The NVM API key to use for the request. * @returns A promise that resolves to the JSON response from the server. */ - public async downloadFiles(fileDid: string, nvmApiKey?: string) { + public async downloadFiles(fileDid: string, destination?: string, nvmApiKey?: string) { const body = { fileDid } const options = { method: 'POST', @@ -702,7 +706,35 @@ export class Payments { throw Error(response.statusText) } - return response.json() + let filename = 'downloaded_file' + const contentDisposition = response.headers.get('content-disposition') + if (contentDisposition) { + const filenameMatch = contentDisposition.match(/filename="(.+?)"/) + if (filenameMatch && filenameMatch[1]) { + filename = filenameMatch[1] + } + } + + if (destination) { + await new Promise((resolve, reject) => { + // @ts-expect-error fs is not available in the browser + fs.mkdirSync(destination, { recursive: true }) + const fileStream = fs.createWriteStream(`${destination}${filename}`) + // @ts-expect-error tsc doesn't know about pipe + response.body?.pipe(fileStream) + // @ts-expect-error tsc doesn't know about pipe + response.body?.on('error', reject) + fileStream.on('finish', resolve) + fileStream.on('close', resolve) + }) + } else { + const buff = await response.arrayBuffer() + fileDownload(buff, filename) + destination = process.cwd() + } + const d = path.join(destination, filename) + return d + // return response.json() } /** diff --git a/yarn.lock b/yarn.lock index 68fad8f..68b0a0d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1571,6 +1571,11 @@ jju@~1.4.0: resolved "https://registry.yarnpkg.com/jju/-/jju-1.4.0.tgz#a3abe2718af241a2b2904f84a625970f389ae32a" integrity sha512-8wb9Yw966OSxApiCt0K3yNJL8pnNeIv+OEq2YMidz4FKP6nonSRoOXc80iXY4JaN2FC11B9qsNmDsm+ZOfMROA== +js-file-download@^0.4.12: + version "0.4.12" + resolved "https://registry.yarnpkg.com/js-file-download/-/js-file-download-0.4.12.tgz#10c70ef362559a5b23cdbdc3bd6f399c3d91d821" + integrity sha512-rML+NkoD08p5Dllpjo0ffy4jRHeY6Zsapvr/W86N7E0yuzAO6qa5X9+xog6zQNlH102J7IXljNY2FtS6Lj3ucg== + "js-tokens@^3.0.0 || ^4.0.0": version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" @@ -2109,7 +2114,16 @@ slash@^3.0.0: resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== -"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0: +"string-width-cjs@npm:string-width@^4.2.0": + version "4.2.3" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" + integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== + dependencies: + emoji-regex "^8.0.0" + is-fullwidth-code-point "^3.0.0" + strip-ansi "^6.0.1" + +string-width@^4.1.0: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -2173,7 +2187,14 @@ string.prototype.trimstart@^1.0.8: define-properties "^1.2.1" es-object-atoms "^1.0.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1": + version "6.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" + integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== + dependencies: + ansi-regex "^5.0.1" + +strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==