Skip to content

Commit

Permalink
More shared types (#30)
Browse files Browse the repository at this point in the history
* Make MSN a shared type

* Move ProblemJSON to shared type
  • Loading branch information
tomas-zijdemans-vipps authored Jan 15, 2024
1 parent 92fe499 commit 70d4d6a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 95 deletions.
14 changes: 4 additions & 10 deletions src/apis/types/checkout_types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
export type CheckoutProblemJSON = {
type?: string | null;
title?: string | null;
status?: number | null;
detail?: string | null;
instance?: string | null;
};
import { MerchantSerialNumber, ProblemJSON } from "./shared_types.ts";

export type CheckoutErrorResponse = CheckoutProblemJSON & {
/////////////// Error Types ///////////////
export type CheckoutErrorResponse = ProblemJSON & {
errorCode: string;
errors: {
[key: string]: string[];
Expand Down Expand Up @@ -470,8 +465,7 @@ export type CheckoutResponsePaymentDetails = {
export type CheckoutSessionOKResponse = {
/** The Id of the session. Example: "v52EtjZriRmGiKiAKHByK2". */
sessionId: string;
/** The merchant's serial number. Example: "123456" */
merchantSerialNumber?: string | null;
merchantSerialNumber?: MerchantSerialNumber | null;
/** The merchant's unique reference for the transaction. Also known as OrderId. Example: "acme-shop-123-order123abc". See https://vippsas.github.io/vipps-developer-docs/docs/vipps-developers/common-topics/orderid */
reference: string;
/** The state of the session. Example: "SessionStarted". The state of the payment is in PaymentDetails.State. */
Expand Down
27 changes: 4 additions & 23 deletions src/apis/types/epayment_types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
/////////////// Error Types ///////////////

import { Scope } from "./shared_types.ts";
import { MerchantSerialNumber, ProblemJSON, Scope } from "./shared_types.ts";

export type EPaymentProblemJSON = {
type: string;
title: string;
detail?: string;
status: number;
instance: string;
};
export type EPaymentErrorResponse = EPaymentProblemJSON & {
/////////////// Error Types ///////////////
export type EPaymentErrorResponse = ProblemJSON & {
traceId: string;
extraDetails: {
name: string;
Expand Down Expand Up @@ -678,19 +670,8 @@ export type EPaymentEvent = {
success: boolean;
};

/**
* The merchant serial number (MSN) for the sales unit.
*
* @minLength 4
* @maxLength 6
* @pattern ^[0-9]{4,6}$
* @example "123456"
*/
export type EPaymentMSN = string;

export type EPaymentWebhookEvent = {
/** The merchant serial number (MSN) for the sales unit. */
msn: EPaymentMSN;
msn: MerchantSerialNumber;
} & EPaymentEvent;

export type EPaymentEventName =
Expand Down
6 changes: 4 additions & 2 deletions src/apis/types/login_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ export type LoginErrorResponse = {
* @format int64
*/
error_code?: number;
/** Debug contains debug information.
* This is usually not available and has to be enabled. */
/**
* Debug contains debug information.
* This is usually not available and has to be enabled.
*/
error_debug?: string;
};
22 changes: 5 additions & 17 deletions src/apis/types/qr_types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MerchantSerialNumber, ProblemJSON } from "./shared_types.ts";

/**
* @description Requested image format.
* Supported values: {image/*,image/png, image/svg+xml, text/targetUrl}
Expand Down Expand Up @@ -96,8 +98,7 @@ export type CallbackQrRequest = {
};

export type CallbackQrResponse = {
/** The merchant serial number (MSN) for the sale unit */
merchantSerialNumber?: string;
merchantSerialNumber?: MerchantSerialNumber;
/**
* The merchant defined identifier for a QR code.
* It will be provided in the callback to the merchant when the
Expand Down Expand Up @@ -138,27 +139,14 @@ export type QrWebhookEvent = {
* @example "d8b7d76d-49aa-48b8-90c6-38779372c163"
*/
merchantQrId: string;
/**
* The merchant serial number (MSN) for the sale unit.
*
* @example "123456"
*/
msn: string;
msn: MerchantSerialNumber;
/**
* @example "2023-10-06T10:45:40.3061965Z"
*/
initiatedAt: string;
};

export type QrErrorResponse = {
/** @minLength 1 */
type?: string;
/** @minLength 1 */
title: string;
/** @minLength 1 */
detail: string;
/** @minLength 1 */
instance: string;
export type QrErrorResponse = ProblemJSON & {
invalidParams?: {
/** @minLength 1 */
name: string;
Expand Down
44 changes: 11 additions & 33 deletions src/apis/types/recurring_types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//////////////// Common types /////////////////

import { Scope } from "./shared_types.ts";
import { ProblemJSON, Scope } from "./shared_types.ts";

/**
* Only NOK is supported at the moment. Support for EUR and DKK will be provided in early 2024.
Expand All @@ -27,37 +27,7 @@ export type RecurringTransactionType = "DIRECT_CAPTURE" | "RESERVE_CAPTURE";

export type RecurringErrorResponse = RecurringErrorV3 | RecurringErrorFromAzure;

/**
* Error response
* Error response using the Problem JSON format
*/
export type RecurringErrorV3 = {
/**
* Path to type of error
* @example "https://developer.vippsmobilepay.com/docs/APIs/recurring-api/recurring-api-problems#validation-error"
*/
type?: string;
/**
* Short description of the error
* @example "Bad Request"
*/
title?: string;
/**
* HTTP status returned with the problem
* @format int32
* @example 400
*/
status?: number;
/**
* Details about the error
* @example "Input validation failed"
*/
detail?: string;
/**
* The path of the request
* @example "/v3/agreements"
*/
instance?: string;
export type RecurringErrorV3 = ProblemJSON & {
/**
* An unique ID for the request
* @example "f70b8bf7-c843-4bea-95d9-94725b19895f"
Expand Down Expand Up @@ -325,6 +295,14 @@ export type ChargeSummary = {
cancelled: number;
};

export type ChargeEventType =
| "CREATE"
| "RESERVE"
| "CAPTURE"
| "REFUND"
| "CANCEL"
| "FAIL";

/** Describes the operation that was performed on the charge */
export type ChargeEvent = {
/**
Expand All @@ -335,7 +313,7 @@ export type ChargeEvent = {
*/
occurred: string;
/** @example "RESERVE" */
event: "CREATE" | "RESERVE" | "CAPTURE" | "REFUND" | "CANCEL" | "FAIL";
event: ChargeEventType;
/**
* The amount related to the operation.
* Amounts are specified in minor units.
Expand Down
39 changes: 39 additions & 0 deletions src/apis/types/shared_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,42 @@ export type ValidUserScopes =
| "delegatedConsents";

export type Scope = UnionConcat<ValidUserScopes, " ">;

/**
* The merchant serial number (MSN) for the sales unit.
*
* @minLength 4
* @maxLength 6
* @pattern ^[0-9]{4,6}$
* @example "123456"
*/
export type MerchantSerialNumber = string;

export type ProblemJSON = {
/**
* Path to type of error
* @example "https://developer.vippsmobilepay.com/docs/APIs/recurring-api/recurring-api-problems#validation-error"
*/
type?: string | null;
/**
* Short description of the error
* @example "Bad Request"
*/
title?: string | null;
/**
* HTTP status returned with the problem
* @format int32
* @example 400
*/
status?: number | null;
/**
* Details about the error
* @example "Input validation failed"
*/
detail?: string | null;
/**
* The path of the request
* @example "/v3/agreements"
*/
instance?: string | null;
};
12 changes: 3 additions & 9 deletions src/apis/types/webhooks_types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// Error types //
export type WebhooksProblemJSON = {
type: string | null;
title: string | null;
status: number | null;
detail: string | null;
instance: string | null;
};
import { ProblemJSON } from "./shared_types.ts";

export type WebhooksErrorResponse = WebhooksProblemJSON & {
// Error types //
export type WebhooksErrorResponse = ProblemJSON & {
extraDetails: {
name: string;
reason: string;
Expand Down
3 changes: 2 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export const parseError = <TErr>(
"detail" in error && "instance" in error
) {
const qrError = error as QrErrorResponse;
const message = qrError.invalidParams?.[0]?.reason ?? qrError.detail;
const message = qrError.invalidParams?.[0]?.reason ?? qrError.detail ??
"Unknown error";
return {
ok: false,
message,
Expand Down

0 comments on commit 70d4d6a

Please sign in to comment.