Skip to content

Commit

Permalink
Merge pull request #5 from kinde-oss/ci/workflow
Browse files Browse the repository at this point in the history
ci: add workflow
  • Loading branch information
DanielRivers authored Nov 19, 2024
2 parents b1c1009 + 7641928 commit 3424cab
Show file tree
Hide file tree
Showing 9 changed files with 880 additions and 39 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/build-test-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build and test

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
main:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
with:
version: 8
- name: Setting up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "pnpm"
- name: Enabling pre-post scripts
run: pnpm config set enable-pre-post-scripts true
- run: pnpm install
- run: pnpm lint
- name: Cache pnpm modules
uses: actions/cache@v4
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
- run: pnpm build
- run: pnpm test:coverage
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
coverage
.release-it.json
CHANGELOG.md
pnpm.lock.yaml
pnpm-lock.yaml
package.json
11 changes: 11 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

/** @type {import('eslint').Linter.Config[]} */
export default [
{ files: ["**/*.{js,mjs,cjs,ts}"] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
38 changes: 35 additions & 3 deletions lib/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getEnvironmentVariable,
createKindeAPI,
WorkflowEvents,
WorkflowTrigger,
} from "./main";

global.kinde = {
Expand All @@ -25,6 +26,38 @@ global.kinde = {
fetch: vi.fn().mockResolvedValue({}),
};

const mockEvent: WorkflowEvents = {
request: {
ip: "1.2.3.4",
auth: {
audience: ["https://api.example.com/v1"],
},
},
context: {
auth: {
origin: "refresh_token_request",
connectionId: "conn_0192b...",
isExistingSession: false,
},
user: {
id: "kp_6a071...",
identityId: "identity_0192c...",
},
domains: {
kindeDomain: "https://mykindebusiness.kinde.com",
},
workflow: {
trigger: WorkflowTrigger.UserTokenGeneration,
},
application: {
clientId: "f77dbc...",
},
organization: {
code: "org_b5a9c8...",
},
},
};

describe("ID Token", () => {
it("should return a proxy object with IdToken properties", () => {
const idTokenHandle = idTokenCustomClaims();
Expand Down Expand Up @@ -65,12 +98,11 @@ describe("getEnvironmentVariable", () => {

describe("createKindeAPI", () => {
it("should return the value of the environment variable", async () => {
const env = await createKindeAPI("API_KEY" as WorkflowEvents, {
method: "GET",
});
const env = await createKindeAPI(mockEvent);
expect(env).toStrictEqual({
delete: expect.any(Function),
get: expect.any(Function),
patch: expect.any(Function),
post: expect.any(Function),
put: expect.any(Function),
});
Expand Down
77 changes: 56 additions & 21 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,97 @@ import { version as packageVersion } from "../package.json";

export const version = packageVersion;

// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace kinde {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function fetch(url: string, options: unknown): Promise<any>;

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace env {
export function get(key: string): { value: string; isSecret: boolean };
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace idToken {
export function setCustomClaim(key: string, value: unknown): void;
export function getCustomClaims(): unknown;
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace accessToken {
export function setCustomClaim(key: string, value: unknown): void;
export function getCustomClaims(): unknown;
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace m2mToken {
export function setCustomClaim(key: string, value: unknown): void;
export function getCustomClaims(): unknown;
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace auth {
export function denyAccess(reason: string): void;
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace risk {
export function setScore(score: number): void;
export function getScore(): number;
}
}

const idTokenClaimsHandler = {
get(target: any, prop: string, receiver: any) {
return Reflect.get(target, prop.toString(), receiver);
const idTokenClaimsHandler: ProxyHandler<Record<string, unknown>> = {
get(
target: Record<string, unknown>,
prop: string,
value: ProxyHandler<Record<string, unknown>>,
) {
return Reflect.get(target, prop.toString(), value);
},
set(target: any, prop: string, receiver: any) {
kinde.idToken.setCustomClaim(prop, receiver);
return Reflect.set(target, prop, receiver);
set(
target: Record<string, unknown>,
prop: string,
value: ProxyHandler<Record<string, unknown>>,
) {
kinde.idToken.setCustomClaim(prop, value);
return Reflect.set(target, prop, value);
},
};

const accessTokenClaimsHandler = {
get(target: any, prop: string, receiver: any) {
return Reflect.get(target, prop.toString(), receiver);
get(
target: Record<string, unknown>,
prop: string,
value: ProxyHandler<Record<string, unknown>>,
) {
return Reflect.get(target, prop.toString(), value);
},
set(target: any, prop: string, receiver: any) {
kinde.accessToken.setCustomClaim(prop, receiver);
return Reflect.set(target, prop, receiver);
set(
target: Record<string, unknown>,
prop: string,
value: ProxyHandler<Record<string, unknown>>,
) {
kinde.accessToken.setCustomClaim(prop, value);
return Reflect.set(target, prop, value);
},
};

const m2mTokenClaimsHandler = {
get(target: any, prop: string, receiver: any) {
return Reflect.get(target, prop.toString(), receiver);
get(
target: Record<string, unknown>,
prop: string,
value: ProxyHandler<Record<string, unknown>>,
) {
return Reflect.get(target, prop.toString(), value);
},
set(target: any, prop: string, receiver: any) {
kinde.m2mToken.setCustomClaim(prop, receiver);
return Reflect.set(target, prop, receiver);
set(
target: Record<string, unknown>,
prop: string,
value: ProxyHandler<Record<string, unknown>>,
) {
kinde.m2mToken.setCustomClaim(prop, value);
return Reflect.set(target, prop, value);
},
};

Expand Down Expand Up @@ -167,6 +201,7 @@ export function denyAccess(reason: string) {
* Fetch data from an external API
* @param reason Reason for denying access
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function fetch<T = any>(
url: string,
options: KindeFetchOptions,
Expand Down Expand Up @@ -275,14 +310,14 @@ export async function createKindeAPI(

return {
get: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "GET", ...params}),
await callKindeAPI({ method: "GET", ...params }),
post: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "POST", ...params}),
await callKindeAPI({ method: "POST", ...params }),
patch: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "PATCH", ...params}),
await callKindeAPI({ method: "PATCH", ...params }),
put: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "PUT", ...params}),
await callKindeAPI({ method: "PUT", ...params }),
delete: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "DELETE", ...params}),
await callKindeAPI({ method: "DELETE", ...params }),
};
}
11 changes: 6 additions & 5 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-empty-object-type */
export type WorkflowSettings = {
/**
* {string} id The unique identifier of the workflow
Expand Down Expand Up @@ -129,8 +130,8 @@ export type KindeFetchOptions = {
};

export type KindeAPIRequest = {
method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH",
endpoint: string,
params?: Record<string, string>,
contentType?: "application/json" | "application/x-www-form-urlencoded"
}
method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH";
endpoint: string;
params?: Record<string, string>;
contentType?: "application/json" | "application/x-www-form-urlencoded";
};
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kinde/infrastructure",
"version": "0.1.0",
"version": "0.1.1-1",
"private": false,
"type": "module",
"module": "dist/infrastructure.js",
Expand All @@ -9,14 +9,24 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"prepublishOnly": "npm run build",
"preview": "vite preview",
"lint": "prettier --check . && eslint .",
"lint:fix": "prettier --write .",
"test": "vitest",
"test:coverage": "vitest --coverage"
},
"files": [
"dist",
"README.md"
],
"devDependencies": {
"@eslint/js": "^9.15.0",
"@vitest/coverage-v8": "^2.1.5",
"eslint": "^9.15.0",
"globals": "^15.12.0",
"typescript": "^5.6.3",
"typescript-eslint": "^8.15.0",
"vite": "^5.4.11",
"vitest": "^2.1.5"
},
Expand Down
Loading

0 comments on commit 3424cab

Please sign in to comment.