From 88b9a25b7cc7984e923af361a6f53a3c80f08329 Mon Sep 17 00:00:00 2001 From: Nicolas Schlecker Date: Thu, 8 Apr 2021 13:58:08 +0200 Subject: [PATCH] Switched to axios --- package.json | 3 ++- src/api.ts | 66 ++++++++++++++++++++++++---------------------------- src/utils.ts | 4 ++-- yarn.lock | 12 ++++++++++ 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index c8e522f..ab6fa6c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.4", + "version": "0.1.5", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", @@ -56,6 +56,7 @@ "typescript": "^4.2.3" }, "dependencies": { + "axios": "^0.21.1", "i18n-iso-countries": "^6.6.0", "joi": "^17.4.0", "jsonwebtoken": "^8.5.1", diff --git a/src/api.ts b/src/api.ts index 6aafce9..7d52487 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,7 @@ import { CorsignPayload, CorsignToken } from './token'; +import axios, { AxiosError } from 'axios'; -const corsignApiUrl = 'https://corsign.de/v1'; +const corsignApiUrl = 'https://api.corsign.de/v1'; export interface GenerateSignedCorsignTokenResponse { /** @@ -27,28 +28,24 @@ export const generateSignedCorsignToken = async ( signerToken: string, tokenId: string, apiUrl: string = corsignApiUrl -): Promise => { - const response = await fetch(`${apiUrl}/sign`, { - method: 'POST', - headers: { - 'X-SIGNER-TOKEN': signerToken, - 'X-TOKEN-ID': tokenId, - 'content-type': 'application/json;charset=UTF-8', - }, - body: JSON.stringify(payload), - }); - - if (response.ok) { - const jsonString = await response.text(); - const data = JSON.parse( - jsonString - ) as GenerateSignedCorsignTokenResponse; - - return data; - } else { - return Promise.reject(); - } -}; +): Promise => + axios + .post(`${apiUrl}/sign`, { + headers: { + 'X-SIGNER-TOKEN': signerToken, + 'X-TOKEN-ID': tokenId, + 'content-type': 'application/json;charset=UTF-8', + }, + body: JSON.stringify(payload), + }) + .then(response => { + if (response.status >= 200 && response.status <= 299) { + return response.data as GenerateSignedCorsignTokenResponse; + } else { + return Promise.reject(response.statusText); + } + }) + .catch((err: AxiosError) => Promise.reject(err.message)); /** * @@ -59,15 +56,14 @@ export const generateSignedCorsignToken = async ( export const validateCorsignToken = async ( token: string, apiUrl: string = corsignApiUrl -): Promise => { - const response = await fetch(`${apiUrl}/validate/${token}`); - - if (response.ok) { - const jsonString = await response.text(); - const data = JSON.parse(jsonString) as CorsignToken; - - return data; - } else { - return Promise.reject(); - } -}; +): Promise => + axios + .get(`${apiUrl}/validate/${token}`) + .then(response => { + if (response.status >= 200 && response.status <= 299) { + return response.data as CorsignToken; + } else { + return Promise.reject(response.statusText); + } + }) + .catch((err: AxiosError) => Promise.reject(err.message)); diff --git a/src/utils.ts b/src/utils.ts index 4ed8179..535a777 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,7 +2,7 @@ import { CorsignPayloadPerson, CorsignToken } from './token'; import jwt from 'jsonwebtoken'; import QRCode from 'qrcode'; -const corsignValidationUrl = 'https://corsign.de/token'; +const corsignValidationUrl = 'https://corsign.de/v1/validate'; /** * @@ -36,7 +36,7 @@ export const decodeCorsignToken = (token: string): CorsignToken => /** * * @param token Any {@link CorsignToken} - * @param validationUrl The url the qr code should point to defaults to https://corsign.de/token/{token} + * @param validationUrl The url the qr code should point to defaults to https://corsign.de/v1/validate/{token} * @returns Data URI containing a representation of the QR Code image. */ export const generateCorsignQrCode = async ( diff --git a/yarn.lock b/yarn.lock index 4a18b83..316497a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1884,6 +1884,13 @@ axe-core@^4.0.2: resolved "https://registry.npmjs.org/axe-core/-/axe-core-4.1.3.tgz" integrity sha512-vwPpH4Aj4122EW38mxO/fxhGKtwWTMLDIJfZ1He0Edbtjcfna/R3YB67yVhezUMzqc3Jr3+Ii50KRntlENL4xQ== +axios@^0.21.1: + version "0.21.1" + resolved "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz" + integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA== + dependencies: + follow-redirects "^1.10.0" + axobject-query@^2.2.0: version "2.2.0" resolved "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz" @@ -3921,6 +3928,11 @@ flush-write-stream@^1.0.0: inherits "^2.0.3" readable-stream "^2.3.6" +follow-redirects@^1.10.0: + version "1.13.3" + resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz" + integrity sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA== + for-in@^1.0.2: version "1.0.2" resolved "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz"