Skip to content

Commit

Permalink
Switched to axios
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNico committed Apr 8, 2021
1 parent 420e2e0 commit 88b9a25
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.4",
"version": "0.1.5",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -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",
Expand Down
66 changes: 31 additions & 35 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand All @@ -27,28 +28,24 @@ export const generateSignedCorsignToken = async (
signerToken: string,
tokenId: string,
apiUrl: string = corsignApiUrl
): Promise<GenerateSignedCorsignTokenResponse> => {
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<GenerateSignedCorsignTokenResponse> =>
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));

/**
*
Expand All @@ -59,15 +56,14 @@ export const generateSignedCorsignToken = async (
export const validateCorsignToken = async (
token: string,
apiUrl: string = corsignApiUrl
): Promise<CorsignToken> => {
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<CorsignToken> =>
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));
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
*
Expand Down Expand Up @@ -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 (
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 88b9a25

Please sign in to comment.