Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-zijdemans-vipps committed Sep 4, 2024
2 parents 834a4a1 + a6f947a commit 749ee46
Show file tree
Hide file tree
Showing 45 changed files with 374 additions and 403 deletions.
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug** A clear and concise description of what the bug is.
Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.** A clear and
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
coverage/
.env
npm/
npm/
deno.lock
2 changes: 1 addition & 1 deletion sample_code/login_sample.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "https://deno.land/std@0.217.0/dotenv/load.ts";
import {
Client,
LoginAuthQueryParams,
type LoginAuthQueryParams,
} from "https://deno.land/x/vipps_mobilepay_sdk@1.2.0/mod.ts";
import { open } from "https://deno.land/x/open@v0.0.6/index.ts";

Expand Down
2 changes: 1 addition & 1 deletion scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { build, emptyDir } from "./script_deps.ts";
import { build, emptyDir } from "@deno/dnt";

/**
* This script builds the SDK for NPM.
Expand Down
23 changes: 11 additions & 12 deletions scripts/publish_latest_tag_to_npm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { colors, greaterThan, parse } from "./script_deps.ts";
import { bold, gray, green, white } from "@std/fmt/colors";
import { greaterThan, parse } from "@std/semver";
import { run } from "./run.ts";

const PACKAGE_NAME = `@vippsmobilepay/sdk`;
Expand All @@ -8,30 +9,30 @@ await run(`git fetch --tags`, `Fetching latest tags...`, true);
// Finding latest tagged commit across branches
const latestTaggedCommit = await run(`git rev-list --tags --max-count=1`);
const trimmedCommit = latestTaggedCommit.trim();
console.log(colors.gray(`Latest tag found ${trimmedCommit}`));
console.log(gray(`Latest tag found ${trimmedCommit}`));

// Finding latest tag name
const latestTagName = await run(`git describe --tags ${trimmedCommit}`);
const trimmedTag = latestTagName.trim();
console.log(colors.gray(`Latest tag name: ${trimmedTag}`));
console.log(gray(`Latest tag name: ${trimmedTag}`));

const latestNpmVersion = await run(
`npm view ${PACKAGE_NAME} version`,
`Fetching latest published npm version...`,
);
console.log(colors.gray(`Latest npm version: ${latestNpmVersion}`));
console.log(gray(`Latest npm version: ${latestNpmVersion}`));

try {
const latestSemVer = parse(trimmedTag);
const latestNpmSemVer = parse(latestNpmVersion);
const tagIsGreaterThanNpm = greaterThan(latestSemVer, latestNpmSemVer);
if (!tagIsGreaterThanNpm) {
console.log(
colors.white(
white(
`📀 Latest tag version ${trimmedTag}. 📮 Latest npm version ${latestNpmVersion}`,
),
);
console.log(colors.white(`No need to publish. Bye! 👋`));
console.log(white(`No need to publish. Bye! 👋`));
Deno.exit(0);
}
} catch (error) {
Expand All @@ -40,12 +41,10 @@ try {
Deno.exit(1);
}
const build = prompt(
colors.green(
colors.bold(`Do you want to build ${trimmedTag} for npm? (y/n)`),
),
green(bold(`Do you want to build ${trimmedTag} for npm? (y/n)`)),
);
if (build?.toLowerCase().trim() !== "y") {
console.log(colors.white(`No problemo. Bye! 👋`));
console.log(white(`No problemo. Bye! 👋`));
Deno.exit(0);
}

Expand All @@ -57,5 +56,5 @@ console.log(buildOutput);
console.log(" ");

// Ask the user to publish, this is a manual step for now
console.log(colors.white(`🚀 Ready to publish, run; 🚀`));
console.log(colors.white(`npm publish ./npm --access public`));
console.log(white(`🚀 Ready to publish, run; 🚀`));
console.log(white(`npm publish ./npm --access public`));
2 changes: 1 addition & 1 deletion scripts/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Spinner } from "./script_deps.ts";
import { Spinner } from "@std/cli";

type CommandResult = {
ok: true;
Expand Down
4 changes: 2 additions & 2 deletions src/apis/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestData } from "../types.ts";
import { AccessToken, AccessTokenError } from "./types/auth_types.ts";
import type { RequestData } from "../types.ts";
import type { AccessToken, AccessTokenError } from "./types/auth_types.ts";

/**
* Factory for creating authentication-related request data.
Expand Down
4 changes: 2 additions & 2 deletions src/apis/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestData } from "../types.ts";
import {
import type { RequestData } from "../types.ts";
import type {
CheckoutErrorResponse,
CheckoutInitiateSessionOKResponse,
CheckoutInitiateSessionRequest,
Expand Down
4 changes: 2 additions & 2 deletions src/apis/epayment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { uuid } from "../deps.ts";
import { RequestData } from "../types.ts";
import {
import type { RequestData } from "../types.ts";
import type {
EPaymentCreatePaymentOKResponse,
EPaymentCreatePaymentRequest,
EPaymentErrorResponse,
Expand Down
4 changes: 2 additions & 2 deletions src/apis/login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestData } from "../types.ts";
import {
import type { RequestData } from "../types.ts";
import type {
LoginErrorResponse,
LoginWellKnownResponse,
} from "./types/login_types.ts";
Expand Down
4 changes: 2 additions & 2 deletions src/apis/ordermanagement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
OrderManagementAddImageOKResponse,
OrderManagementErrorResponse,
OrderManagementGetOrderOKResponse,
Expand All @@ -9,7 +9,7 @@ import {
OrderManagementPaymentType,
OrderManagementReceipt,
} from "./types/ordermanagement_types.ts";
import { RequestData } from "../types.ts";
import type { RequestData } from "../types.ts";

/**
* Factory object for creating request data for the Order Management API.
Expand Down
4 changes: 2 additions & 2 deletions src/apis/qr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestData } from "../types.ts";
import {
import type { RequestData } from "../types.ts";
import type {
CallbackQrImageFormat,
CallbackQrImageSize,
CallbackQrRequest,
Expand Down
4 changes: 2 additions & 2 deletions src/apis/recurring.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestData } from "../types.ts";
import {
import type { RequestData } from "../types.ts";
import type {
RecurringAgreementResponseV3,
RecurringAsyncChargeResponse,
RecurringChargeReference,
Expand Down
4 changes: 2 additions & 2 deletions src/apis/types/checkout_types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MerchantSerialNumber } from "../../types.ts";
import { ProblemJSON } from "./shared_types.ts";
import type { MerchantSerialNumber } from "../../types.ts";
import type { ProblemJSON } from "./shared_types.ts";

/////////////// Error Types ///////////////
export type CheckoutErrorResponse = ProblemJSON & {
Expand Down
4 changes: 2 additions & 2 deletions src/apis/types/epayment_types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MerchantSerialNumber } from "../../types.ts";
import { ProblemJSON, Scope } from "./shared_types.ts";
import type { MerchantSerialNumber } from "../../types.ts";
import type { ProblemJSON, Scope } from "./shared_types.ts";

/////////////// Error Types ///////////////
export type EPaymentErrorResponse = ProblemJSON & {
Expand Down
2 changes: 1 addition & 1 deletion src/apis/types/login_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Scope } from "./shared_types.ts";
import type { Scope } from "./shared_types.ts";

/**
* ErrorResponse
Expand Down
4 changes: 2 additions & 2 deletions src/apis/types/qr_types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MerchantSerialNumber } from "../../types.ts";
import { ProblemJSON } from "./shared_types.ts";
import type { MerchantSerialNumber } from "../../types.ts";
import type { ProblemJSON } from "./shared_types.ts";

/**
* Represents the response for a QR error.
Expand Down
2 changes: 1 addition & 1 deletion src/apis/types/recurring_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProblemJSON, Scope } from "./shared_types.ts";
import type { ProblemJSON, Scope } from "./shared_types.ts";

///////////////// Error types /////////////////

Expand Down
2 changes: 1 addition & 1 deletion src/apis/types/webhooks_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProblemJSON } from "./shared_types.ts";
import type { ProblemJSON } from "./shared_types.ts";

// Error types //
export type WebhooksErrorResponse = ProblemJSON & {
Expand Down
4 changes: 2 additions & 2 deletions src/apis/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestData } from "../types.ts";
import { UserInfo, UserInfoError } from "./types/user_types.ts";
import type { RequestData } from "../types.ts";
import type { UserInfo, UserInfoError } from "./types/user_types.ts";

/**
* Factory object for creating Userinfo API requests.
Expand Down
4 changes: 2 additions & 2 deletions src/apis/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestData } from "../types.ts";
import {
import type { RequestData } from "../types.ts";
import type {
WebhooksErrorResponse,
WebhooksGetRegisteredOKResponse,
WebhooksRegisterOKResponse,
Expand Down
26 changes: 16 additions & 10 deletions src/base_client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
BaseClient,
ClientConfig,
ClientResponse,
Expand All @@ -12,34 +12,40 @@ import { fetchRetry } from "./fetch.ts";
/**
* Creates a base client with the given configuration.
*
* @param cfg - The client configuration.
* @returns The base client.
* @param {ClientConfig} cfg - The client configuration.
* @returns {BaseClient} The base client.
*/
export const baseClient = (cfg: ClientConfig): BaseClient =>
({
/**
* Makes a request to the server.
*
* @template TOk The type of the successful response.
* @template TErr The type of the error response.
* @param requestData The request data.
* @returns A promise that resolves to a ClientResponse object containing the response data.
* @template TOk - The type of the successful response.
* @template TErr - The type of the error response.
* @param {RequestData<TOk, TErr>} requestData - The request data.
* @returns {Promise<ClientResponse<TOk, TErr>>} A promise that resolves to a ClientResponse object containing the response data.
*/
async makeRequest<TOk, TErr>(
requestData: RequestData<TOk, TErr>,
): Promise<ClientResponse<TOk, TErr>> {
// Validate the request data
// Validate the request data
const validationError = validateRequestData(requestData, cfg);
if (validationError) {
return { ok: false, error: { message: validationError } };
}

// Build the request
const request = buildRequest(cfg, requestData);

try {
const res = await fetchRetry<TOk, TErr>(request, cfg.retryRequests);
return res;
// Make the request with retry logic
const response = await fetchRetry<TOk, TErr>(
request,
cfg.retryRequests,
);
return response;
} catch (error: unknown) {
// Parse and return the error
return parseError<TErr>(error);
}
},
Expand Down
34 changes: 19 additions & 15 deletions src/base_client_helper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { filterKeys, uuid } from "./deps.ts";
import {
import { filterKeys } from "./deps.ts";
import type {
ClientConfig,
DefaultHeaders,
OmitHeaders,
RequestData,
} from "./types.ts";

import { uuid } from "./deps.ts";

/**
* Builds a Request object based on the provided configuration and request data.
*
* @param cfg - The client configuration.
* @param requestData - The request data containing method, headers, token, body, and URL.
* @returns A Request object.
* @param {ClientConfig} cfg - The client configuration.
* @param {RequestData<unknown, unknown>} requestData - The request data containing method, headers, token, body, and URL.
* @returns {Request} A Request object.
*/
export const buildRequest = (
cfg: ClientConfig,
Expand All @@ -37,12 +39,11 @@ export const buildRequest = (
/**
* Returns a headers object based on the provided client configuration.
*
* @param cfg - The client configuration.
* @param additionalHeaders - Additional headers to include,
* these will not override default headers.
* @param omitHeaders - Headers to omit from the returned object.
* @param token - The token to use in the Authorization header.
* @returns A headers object.
* @param {ClientConfig} cfg - The client configuration.
* @param {string} [token] - The token to use in the Authorization header.
* @param {Record<string, string>} [additionalHeaders] - Additional headers to include, these will not override default headers.
* @param {OmitHeaders} [omitHeaders=[]] - Headers to omit from the returned object.
* @returns {Record<string, string>} A headers object.
*/
export const getHeaders = (
cfg: ClientConfig,
Expand Down Expand Up @@ -78,7 +79,8 @@ export const getHeaders = (

/**
* Returns the user agent string for the client.
* @returns The user agent string.
*
* @returns {string} The user agent string.
*/
export const getUserAgent = (): string => {
// If the sdk is loaded using require, import.meta.url will be undefined
Expand All @@ -92,8 +94,9 @@ export const getUserAgent = (): string => {
* Creates a user agent string based on the provided meta URL.
* The function is meant to receive import.meta.url (that will returns the URL of the current module).
* Read more in the Deno docs in Import Meta
* @param metaUrl - The meta URL of the module.
* @returns The user agent string.
*
* @param {string | undefined} metaUrl - The meta URL of the module.
* @returns {string} The user agent string.
*/
export const createSDKUserAgent = (metaUrl: string | undefined): string => {
if (!metaUrl) {
Expand All @@ -113,6 +116,7 @@ export const createSDKUserAgent = (metaUrl: string | undefined): string => {
} // Or if the module was loaded from npm
else if (url.pathname.includes("node_modules")) {
return `Vipps/Deno SDK/npm-module`;
} // Otherwise, we don't know where the module was loaded from
}
// Otherwise, we don't know where the module was loaded from
return `Vipps/Deno SDK/unknown`;
};
7 changes: 6 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RetryError } from "./deps.ts";
import { STATUS_CODE } from "./deps.ts";
import { SDKError } from "./types.ts";
import type { SDKError } from "./types.ts";

/**
* Parses the error and returns an object with error details.
Expand All @@ -9,6 +9,11 @@ import { SDKError } from "./types.ts";
* connection errors, forbidden status codes, and generic errors. It returns
* a standardized error object that can be used throughout the application.
*
*
* This function handles different types of errors, including retry errors,
* connection errors, forbidden status codes, and generic errors. It returns
* a standardized error object that can be used throughout the application.
*
* @template TErr - The type of the error object.
* @param error - The error to be parsed.
* @returns An object with error details.
Expand Down
Loading

0 comments on commit 749ee46

Please sign in to comment.