Initial commit

This commit is contained in:
Pal Kerecsenyi 2023-12-23 12:05:21 +00:00
commit 83cf577e4f
Signed by: palk
GPG Key ID: 6891661E25394C2C
11 changed files with 15446 additions and 0 deletions

29
.eslintrc.js Normal file

@ -0,0 +1,29 @@
module.exports = {
env: {
es2021: true,
node: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
plugins: ["@typescript-eslint"],
overrides: [
{
env: {
node: true,
},
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
rules: {},
}

2
.gitignore vendored Normal file

@ -0,0 +1,2 @@
.yarn/*
build/*

10592
.pnp.cjs generated Executable file

File diff suppressed because one or more lines are too long

2090
.pnp.loader.mjs generated Normal file

File diff suppressed because it is too large Load Diff

5
.prettierrc.json Normal file

@ -0,0 +1,5 @@
{
"semi": false,
"singleQuote": false,
"tabWidth": 4
}

3
.yarnrc.yml Normal file

@ -0,0 +1,3 @@
enableGlobalCache: true
nodeLinker: pnp

29
package.json Normal file

@ -0,0 +1,29 @@
{
"name": "@paltiverse/palauth-iam-node",
"version": "1.0.0",
"repository": "https://gitlab.palk.me/paltiverse-public/palauth-iam-node",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.15.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
"eslint-plugin-prettier": "^5.1.1",
"eslint-plugin-promise": "^6.0.0",
"eslint_d": "^13.1.2",
"prettier": "^3.1.1",
"typescript": "^5.3.3"
},
"scripts": {
"build": "tsc",
"prepublishOnly": "yarn run build"
},
"main": "build/index.js",
"types": "build/index.d.ts",
"packageManager": "yarn@4.0.2",
"dependencies": {
"utility-types": "^3.10.0",
"wretch": "^2.7.1"
}
}

40
src/index.ts Normal file

@ -0,0 +1,40 @@
import type { Optional } from "utility-types"
import type { Wretch } from "wretch"
import checkPermission from "./routes/check"
export interface PalAuthIAMConfig {
clientID: string
clientSecret: string
baseURL: string
}
export default class PalAuthIAMClient {
private config: PalAuthIAMConfig
constructor(config: Optional<PalAuthIAMConfig, "baseURL">) {
this.config = {
...config,
baseURL: config.baseURL ?? "https://auth.palk.me/iam",
}
}
public setConfig(config: Partial<PalAuthIAMConfig>) {
this.config = {
...this.config,
...config,
}
}
protected makeRequest<ResponseType extends object>(
req: Wretch,
path: string,
method: "GET" | "POST",
) {
return req
.url(this.config.baseURL + "/" + this.config.clientID + path, true)
.auth(`Bearer ${this.config.clientSecret}`)
.fetch(method)
.json<ResponseType>()
}
public checkPermission = checkPermission.bind(this)
}

24
src/routes/check.ts Normal file

@ -0,0 +1,24 @@
import PalAuthIAMClient from ".."
import wretch from "wretch"
export type Permission = string
export interface CheckPermissionRequest {
userId: string
permission: Permission
}
export interface CheckPermissionResponse {
allowed: boolean
}
export default function checkPermission(
this: PalAuthIAMClient,
req: CheckPermissionRequest,
) {
const params = new URLSearchParams()
params.set("userId", req.userId)
params.set("permission", req.permission)
return this.makeRequest<CheckPermissionResponse>(
wretch(),
"/check?" + params.toString(),
"GET",
)
}

19
tsconfig.json Normal file

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"inlineSourceMap": true,
"inlineSources": true,
"declaration": true,
"esModuleInterop": true,
"outDir": "build",
"target": "es2016",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"strict": true,
},
"include": [
"src/**/*"
]
}

2613
yarn.lock Normal file

File diff suppressed because it is too large Load Diff