Skip to content

Commit

Permalink
added first approach
Browse files Browse the repository at this point in the history
  • Loading branch information
golsch committed Feb 7, 2025
1 parent 7d37830 commit b6c4f7c
Show file tree
Hide file tree
Showing 22 changed files with 2,507 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on: [ push, pull_request ]

jobs:
setup:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- uses: actions/cache@v4
id: yarn-cache
with:
path: node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
run: yarn install

lint:
runs-on: ubuntu-22.04
needs: setup
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
id: yarn-cache
with:
path: node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- name: Run lint test
run: yarn lint

test-publish:
runs-on: ubuntu-22.04
needs: setup
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v4
id: yarn-cache
with:
path: node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- name: Run publish test
run: yarn test-publish

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

node_modules
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MyCoRe-JS

This repository provides JS/TS functionalities for MyCoRe.
40 changes: 40 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import eslint from '@eslint/js';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import tsdoc from 'eslint-plugin-tsdoc';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';

export default tseslint.config(
{
ignores: ['dist/', 'node_modules/'],
},
eslint.configs.recommended,
{
files: ['src/**/*.ts'],
extends: [
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.strictTypeChecked,
],
plugins: {
'@typescript-eslint': tseslint.plugin,
tsdoc,
},
languageOptions: {
globals: globals.browser,
ecmaVersion: 'latest',
sourceType: 'module',
parserOptions: {
project: true,
},
},
rules: {
'tsdoc/syntax': 'warn',
},
},
{
languageOptions: {
globals: globals.browser,
},
},
eslintPluginPrettierRecommended
);
18 changes: 18 additions & 0 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@golsch/test",
"version": "1.0.39",
"exports": {
"./i18n": "./src/i18n/index.ts",
"./auth": "./src/auth/index.ts",
"./common/client": "./src/common/client/index.ts",
"./orcid": "./src/orcid/index.ts",
"./common/cache": "./src/common/cache/index.ts",
"./acl/accesskey": "./src/acl/accesskey/index.ts"
},
"publish": {
"include": [
"src/**/*.ts"
]
}
}

30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "mycore-js",
"version": "1.0.0",
"description": "A javascript library for MyCoRe.",
"main": "index.js",
"repository": "https://github.com/MyCoRe-Org/mycore-js.git",
"author": "MyCoRe",
"license": "GPL-3.0",
"scripts": {
"test-publish": "jsr publish --dry-run",
"lint": "eslint src --max-warnings 0"
},
"devDependencies": {
"@eslint/js": "^9.16.0",
"@typescript-eslint/eslint-plugin": "^8.17.0",
"@typescript-eslint/parser": "^8.17.0",
"eslint": "^9.16.0",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-prettier": "^5.2.3",
"eslint-plugin-tsdoc": "^0.4.0",
"globals": "^15.13.0",
"jsr": "^0.13.2",
"prettier": "^3.4.2",
"typescript": "^5.7.2",
"typescript-eslint": "^8.17.0"
},
"dependencies": {
"axios": "^1.7.9"
}
}
12 changes: 12 additions & 0 deletions prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
semi: true,
singleQuote: true,
printWidth: 80,
trailingComma: 'es5',
tabWidth: 2,
useTabs: false,
endOfLine: 'auto',
arrowParens: 'avoid',
bracketSpacing: true,
bracketSameLine: false,
};
234 changes: 234 additions & 0 deletions src/acl/accesskey/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/*!
* This file is part of *** M y C o R e ***
* See https://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
*/

import { MCRHTTPClient, MCRHTTPResponse } from '../../common/client';

/**
* Represents an access key in the system.
*/
export interface MCRAccessKey {
id: string;
reference: string;
secret: string;
type: string;
isActive: boolean;
comment?: string;
expiration?: number | null;
}

/**
* DTO for creating a new access key.
*/
export interface MCRCreateAccessKeyDTO {
reference: string;
secret: string;
type: string;
isActive: boolean;
comment?: string;
expiration?: string | null;
}

/**
* DTO for updating an access key.
*/
export interface MCRUpdateAccessKeyDTO {
reference: string;
secret: string;
type: string;
isActive: boolean;
comment?: string;
expiration?: number | null;
}

/**
* DTO for partially updating an access key.
*/
export interface MCRPartialUpdateAccessKeyDTO {
reference?: string;
type?: string;
isActive?: boolean;
comment?: string;
expiration?: string | null;
}

/**
* Information about the access keys.
*/
export interface MCRAccessKeySummary {
accessKeys: MCRAccessKey[];
totalCount: number;
}

/**
* Extracts the response data and returns the access key summary.
*
* @param response - The response object containing the data and headers.
* @returns The parsed access keys summary.
*/
const createSummary = (
response: MCRHTTPResponse<MCRAccessKey[]>
): MCRAccessKeySummary => {
const totalCount = response.headers['x-total-count'];
return {
accessKeys: response.data,
totalCount: totalCount ? parseInt(totalCount, 10) : 0,
};
};

/**
* Options for getting access keys.
*/
export interface MCRGetAccessKeysOptions {
permissions?: string[];
reference?: string;
offset?: number;
limit?: number;
}

const API_URL = 'api/v2/access-keys/';

/**
* Service for managing access keys.
*/
export class MCRAccessKeyService {
private client: MCRHTTPClient;

/**
* Creates an instance of AccessKeyService.
*
* @param client - The HTTP client used to make API requests.
*/
constructor(client: MCRHTTPClient) {
this.client = client;
}

/**
* Retrieves a list of access keys.
*
* @param options - The options to filter and paginate the access keys (optional).
* @returns A promise that resolves with the access keys information.
*/
public async getAccessKeys(
options?: MCRGetAccessKeysOptions
): Promise<MCRAccessKeySummary> {
const url = new URL(API_URL);
if (options?.reference) {
url.searchParams.set('reference', options.reference);
}
if (options?.permissions && options.permissions.length > 0) {
url.searchParams.set('permissions', options.permissions.join(','));
}
if (options?.offset) {
url.searchParams.set('offset', String(options.offset));
}
if (options?.limit) {
url.searchParams.set('limit', String(options.limit));
}
try {
return createSummary(
await this.client.get<MCRAccessKey[]>(url.toString())
);
} catch (error) {
throw new Error(`Failed to get access keys: ${(error as Error).message}`);
}
}

/**
* Retrieves a single access key by its ID.
*
* @param id - The ID of the access key.
* @returns A promise that resolves with the access key data.
*/
public async getAccessKey(id: string): Promise<MCRAccessKey> {
try {
return (await this.client.get<MCRAccessKey>(new URL(id, API_URL))).data;
} catch (error) {
throw new Error(`Failed to get access key: ${(error as Error).message}`);
}
}

/**
* Creates a new access key.
*
* @param accessKey - The data for the new access key.
* @returns A promise that resolves with the ID of the created access key.
*/
public async createAccessKey(
accessKey: MCRCreateAccessKeyDTO
): Promise<string> {
try {
const response = await this.client.post(API_URL, accessKey);
return response.headers['location'].split('/').pop() as string;
} catch (error) {
throw new Error(
`Failed to create access key: ${(error as Error).message}`
);
}
}

/**
* Updates an existing access key.
* @param id - The ID of the access key.
* @param accessKey - The data to update the access key with.
*/
public async updateAccessKey(
id: string,
accessKey: MCRUpdateAccessKeyDTO
): Promise<void> {
try {
await this.client.post(new URL(id, API_URL), accessKey);
} catch (error) {
throw new Error(
`Failed to update access key: ${(error as Error).message}`
);
}
}

/**
* Partially updates an existing access key.
*
* @param id - The ID of the access key.
* @param accessKey - The data to update the access key with.
*/
public async patchAccessKey(
id: string,
accessKey: MCRPartialUpdateAccessKeyDTO
): Promise<void> {
try {
await this.client.patch(new URL(id, API_URL), accessKey);
} catch (error) {
throw new Error(
`Failed to patch access key: ${(error as Error).message}`
);
}
}

/**
* Deletes an access key.
* @param id - The ID of the access key to delete.
*/
public async deleteAccessKey(id: string): Promise<void> {
try {
await this.client.delete(new URL(id, API_URL));
} catch (error) {
throw new Error(
`Failed to delete access key: ${(error as Error).message}`
);
}
}
}
Loading

0 comments on commit b6c4f7c

Please sign in to comment.