Skip to content

Commit

Permalink
Remove collections deps (#65)
Browse files Browse the repository at this point in the history
* Prepare JSR: Update fmt, lint and package info in deno.json

* Remove dependency on std/collections
  • Loading branch information
tomas-zijdemans-vipps authored Sep 27, 2024
1 parent 644bd59 commit 1c7aaef
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 23 deletions.
16 changes: 14 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
{
"exports": "./src/mod.ts",
"fmt": {
"include": ["src/", "tests/", "deno.json"]
},
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.41.3",
"@hey-api/openapi-ts": "npm:@hey-api/openapi-ts",
"@hongminhee/deno-mock-fetch": "jsr:@hongminhee/deno-mock-fetch@^0.3.2",
"@lambdalisue/systemopen": "jsr:@lambdalisue/systemopen@^1.0.0",
"@std/assert": "jsr:@std/assert@^1.0.5",
"@std/assert": "jsr:@std/assert@^1.0.6",
"@std/cli": "jsr:@std/cli@^1.0.6",
"@std/dotenv": "jsr:@std/dotenv@^0.225.2",
"@std/fmt": "jsr:@std/fmt@^1.0.2",
"@std/semver": "jsr:@std/semver@^1.0.3"
}
},
"lint": {
"include": ["src/", "tests/"]
},
"name": "@vippsmobilepay/sdk",
"test": {
"include": ["tests/"]
},
"version": "1.0.0"
}
57 changes: 47 additions & 10 deletions src/base_client_helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { filterKeys } from "./deps.ts";
import type {
DefaultHeaders,
OmitHeaders,
Expand Down Expand Up @@ -63,17 +62,55 @@ export const getHeaders = (
"Idempotency-Key": uuid.generate(),
};

// Remove omitted headers
const trimmedHeaders = filterKeys(
defaultHeaders,
(header) => !omitHeaders.includes(header as OmitHeaders[number]),
return createHeaders(defaultHeaders, omitHeaders, additionalHeaders);
};

/**
* Filters out specified headers from the default headers.
*
* @param {Record<string, string>} headers - The headers object to filter.
* @param {string[]} omitHeaders - The list of headers to omit.
* @returns {Record<string, string>} The filtered headers object.
*/
export const filterHeaders = (
headers: Record<string, string>,
omitHeaders: string[],
): Record<string, string> => {
return Object.fromEntries(
Object.entries(headers).filter(([key]) => !omitHeaders.includes(key)),
);
};

// Add additional headers
return {
...additionalHeaders,
...trimmedHeaders,
};
/**
* Adds additional headers to the default headers without overwriting existing headers.
*
* @param {Record<string, string>} defaultHeaders - The default headers object.
* @param {Record<string, string>} additionalHeaders - The additional headers to add.
* @returns {Record<string, string>} The combined headers object.
*/
export const addHeaders = (
defaultHeaders: Record<string, string>,
additionalHeaders: Record<string, string>,
): Record<string, string> => {
return { ...additionalHeaders, ...defaultHeaders };
};

/**
* Creates a new headers object by omitting specified headers from the default headers
* and adding additional headers.
*
* @param {Record<string, string>} defaultHeaders - The default headers object.
* @param {string[]} omitHeaders - The list of headers to omit.
* @param {Record<string, string>} [additionalHeaders={}] - The additional headers to add.
* @returns {Record<string, string>} The new headers object.
*/
export const createHeaders = (
defaultHeaders: Record<string, string>,
omitHeaders: string[],
additionalHeaders: Record<string, string> = {},
): Record<string, string> => {
const combinedHeaders = addHeaders(defaultHeaders, additionalHeaders);
return filterHeaders(combinedHeaders, omitHeaders);
};

/**
Expand Down
1 change: 0 additions & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { filterKeys } from "https://deno.land/std@0.224.0/collections/mod.ts";
/**
* This is a workaround for `crypto.randomUUID` not being available in
* Node.js 18. This will be removed after Node.js 18 reaches End-of-Life
Expand Down
10 changes: 3 additions & 7 deletions src/types_external.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
MakeNestedPropertyOptional,
MakePropertyOptional,
PrettifyType,
Expand All @@ -18,9 +18,7 @@ export type AccessTokenError = {
correlation_id: string;
error_uri: string;
};
export type {
AuthorizationTokenResponse,
} from "./generated_types/access_token/types.gen.ts";
export type { AuthorizationTokenResponse } from "./generated_types/access_token/types.gen.ts";

/**
* Checkout API
Expand Down Expand Up @@ -64,9 +62,7 @@ export type {
/**
* ePayment API
*/
import type {
CreatePaymentRequest as _CreatePaymentRequest,
} from "./generated_types/epayment/types.gen.ts";
import type { CreatePaymentRequest as _CreatePaymentRequest } from "./generated_types/epayment/types.gen.ts";

// Make the reference property optional
export type CreatePaymentRequest = MakePropertyOptional<
Expand Down
2 changes: 1 addition & 1 deletion tests/checkout_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { checkoutRequestFactory } from "../src/apis/checkout.ts";
import type { InitiatePaymentSessionRequest } from "../src/types_external.ts";
import { InitiateSubscriptionSessionRequest } from "../src/mod.ts";
import type { InitiateSubscriptionSessionRequest } from "../src/mod.ts";
import {
assert,
assertEquals,
Expand Down
2 changes: 1 addition & 1 deletion tests/epayment_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert, assertEquals, assertExists } from "@std/assert";
import { ePaymentRequestFactory } from "../src/apis/epayment.ts";
import { uuid } from "../src/deps.ts";
import { CreatePaymentRequest } from "../src/types_external.ts";
import type { CreatePaymentRequest } from "../src/types_external.ts";

Deno.test("ePayment - create - Should have correct url and header", () => {
const expected = {
Expand Down
2 changes: 1 addition & 1 deletion tests/error_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccessTokenError } from "../src/types_external.ts";
import type { AccessTokenError } from "../src/types_external.ts";
import { parseError } from "../src/errors.ts";
import { Client } from "../src/mod.ts";
import { assert, assertExists } from "@std/assert";
Expand Down

0 comments on commit 1c7aaef

Please sign in to comment.