Skip to content

Commit ca7effb

Browse files
committed
improved structure
1 parent 9b4d9cb commit ca7effb

34 files changed

+978
-506
lines changed

jsr.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "@mycore-test/js-common",
3-
"version": "0.1.0",
3+
"version": "0.1.2",
44
"exports": {
55
"./i18n": "./src/i18n/index.ts",
6+
"./access-key": "./src/access-key/index.ts",
67
"./auth": "./src/auth/index.ts",
7-
"./common/client": "./src/common/client/index.ts",
88
"./orcid": "./src/orcid/index.ts",
9-
"./common/cache": "./src/common/cache/index.ts",
10-
"./acl/accesskey": "./src/acl/accesskey/index.ts"
9+
"./utils/cache": "./src/utils/cache/index.ts",
10+
"./utils/errors": "./src/utils/errors/index.ts",
11+
"./utils/http": "./src/utils/http/index.ts"
1112
},
1213
"publish": {
1314
"include": [
Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,27 @@
1616
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
import { HttpClient, HttpResponse } from '../../common/client';
2019
import {
21-
AccessKey,
22-
CreateAccessKeyDto,
23-
UpdateAccessKeyDto,
24-
PartialUpdateAccessKeyDto,
25-
AccessKeySummary,
26-
} from './model';
20+
HttpClient,
21+
HttpResponse,
22+
HttpError,
23+
handleError,
24+
} from '../utils/http';
25+
import { AccessKeyDto } from './dtos/access-key.dto';
26+
import { CreateAccessKeyDto } from './dtos/create-access-key.dto';
27+
import { UpdateAccessKeyDto } from './dtos/update-access-key.dto';
28+
import { PartialUpdateAccessKeyDto } from './dtos/update-access-key-partial.dto';
29+
import { AccessKeysSummaryDto } from './dtos/access-keys-summary.dto';
2730

2831
/**
2932
* Extracts the response data and returns the access key summary.
3033
* @param response - The response object containing the data and headers.
3134
* @returns The parsed access keys summary.
3235
*/
3336
const createSummary = (
34-
response: HttpResponse<AccessKey[]>
35-
): AccessKeySummary => {
36-
const totalCount = response.headers['x-total-count'];
37+
response: HttpResponse<AccessKeyDto[]>
38+
): AccessKeysSummaryDto => {
39+
const totalCount = response.headers.get('x-total-count');
3740
return {
3841
accessKeys: response.data,
3942
totalCount: totalCount ? parseInt(totalCount, 10) : 0,
@@ -91,7 +94,7 @@ export class AccessKeyService {
9194
*/
9295
public async getAccessKeys(
9396
options?: GetAccessKeysOptions
94-
): Promise<AccessKeySummary> {
97+
): Promise<AccessKeysSummaryDto> {
9598
const searchParams = new URLSearchParams();
9699
if (options?.reference) {
97100
searchParams.set('reference', options.reference);
@@ -107,12 +110,15 @@ export class AccessKeyService {
107110
}
108111
try {
109112
return createSummary(
110-
await this.client.get<AccessKey[]>(
113+
await this.client.get<AccessKeyDto[]>(
111114
`${API_URL}?${searchParams.toString()}`
112115
)
113116
);
114117
} catch (error) {
115-
throw new Error(`Failed to get access keys: ${(error as Error).message}`);
118+
if (error instanceof HttpError) {
119+
handleError(error);
120+
}
121+
throw new Error('Failed to get access keys');
116122
}
117123
}
118124

@@ -121,11 +127,14 @@ export class AccessKeyService {
121127
* @param id - The ID of the access key.
122128
* @returns A promise that resolves with the access key data.
123129
*/
124-
public async getAccessKey(id: string): Promise<AccessKey> {
130+
public async getAccessKey(id: string): Promise<AccessKeyDto> {
125131
try {
126-
return (await this.client.get<AccessKey>(`${API_URL}/${id}`)).data;
132+
return (await this.client.get<AccessKeyDto>(`${API_URL}/${id}`)).data;
127133
} catch (error) {
128-
throw new Error(`Failed to get access key: ${(error as Error).message}`);
134+
if (error instanceof HttpError) {
135+
handleError(error);
136+
}
137+
throw new Error('Failed to get access key');
129138
}
130139
}
131140

@@ -137,11 +146,12 @@ export class AccessKeyService {
137146
public async createAccessKey(accessKey: CreateAccessKeyDto): Promise<string> {
138147
try {
139148
const response = await this.client.post(API_URL, accessKey);
140-
return response.headers['location'].split('/').pop() as string;
149+
return response.headers.get('location')?.split('/').pop() as string;
141150
} catch (error) {
142-
throw new Error(
143-
`Failed to create access key: ${(error as Error).message}`
144-
);
151+
if (error instanceof HttpError) {
152+
handleError(error);
153+
}
154+
throw new Error('Failed to create access key');
145155
}
146156
}
147157

@@ -157,9 +167,10 @@ export class AccessKeyService {
157167
try {
158168
await this.client.post(`${API_URL}/${id}`, accessKey);
159169
} catch (error) {
160-
throw new Error(
161-
`Failed to update access key: ${(error as Error).message}`
162-
);
170+
if (error instanceof HttpError) {
171+
handleError(error);
172+
}
173+
throw new Error('Failed to update access key');
163174
}
164175
}
165176

@@ -175,9 +186,10 @@ export class AccessKeyService {
175186
try {
176187
await this.client.patch(`${API_URL}/${id}`, accessKey);
177188
} catch (error) {
178-
throw new Error(
179-
`Failed to patch access key: ${(error as Error).message}`
180-
);
189+
if (error instanceof HttpError) {
190+
handleError(error);
191+
}
192+
throw new Error('Failed to update access key');
181193
}
182194
}
183195

@@ -189,9 +201,10 @@ export class AccessKeyService {
189201
try {
190202
await this.client.delete(`${API_URL}/${id}`);
191203
} catch (error) {
192-
throw new Error(
193-
`Failed to delete access key: ${(error as Error).message}`
194-
);
204+
if (error instanceof HttpError) {
205+
handleError(error);
206+
}
207+
throw new Error('Failed to delete access key');
195208
}
196209
}
197210
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*!
2+
* This file is part of *** M y C o R e ***
3+
* See https://www.mycore.de/ for details.
4+
*
5+
* MyCoRe is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* MyCoRe is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
/**
20+
* Represents an access key in the system.
21+
*/
22+
export interface AccessKeyDto {
23+
/**
24+
* The unique identifier for the access key.
25+
*/
26+
id: string;
27+
28+
/**
29+
* A reference or name associated with the access key.
30+
*/
31+
reference: string;
32+
33+
/**
34+
* The secret associated with the access key used for authentication.
35+
*/
36+
secret: string;
37+
38+
/**
39+
* The permission type for the authentication.
40+
*/
41+
type: string;
42+
43+
/**
44+
* Indicates whether the access key is currently useable.
45+
*/
46+
isActive: boolean;
47+
48+
/**
49+
* An optional comment or description that provides additional context for the access key.
50+
*/
51+
comment?: string;
52+
53+
/**
54+
* An optional expiration timestamp (in Unix time) indicating when the access key expires.
55+
* If `null`, the key does not expire.
56+
*/
57+
expiration?: number | null;
58+
}

src/acl/accesskey/config.ts renamed to src/access-key/dtos/access-keys-summary.dto.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
import { AccessKeyDto } from './access-key.dto';
20+
1921
/**
20-
* Configuration for access keys.
22+
* Information about the access keys.
2123
*/
22-
export interface AccessKeyConfig {
24+
export interface AccessKeysSummaryDto {
2325
/**
24-
* A boolean indicating whether access key for sessions are enabled.
26+
* An array access keys that are part of the summary.
2527
*/
26-
isAccessKeySessionEnabled: boolean;
28+
accessKeys: AccessKeyDto[];
2729

2830
/**
29-
* An array of strings representing the permissions that are allowed for access keys in sessions.
31+
* The total count of all access keys.
3032
*/
31-
allowedAccessKeySessionPermissions: string[];
33+
totalCount: number;
3234
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*!
2+
* This file is part of *** M y C o R e ***
3+
* See https://www.mycore.de/ for details.
4+
*
5+
* MyCoRe is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* MyCoRe is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
/**
20+
* DTO for creating a new access key.
21+
*/
22+
export interface CreateAccessKeyDto {
23+
/**
24+
* A reference or name associated with the access key.
25+
*/
26+
reference: string;
27+
28+
/**
29+
* The secret associated with the access key used for authentication.
30+
*/
31+
secret: string;
32+
33+
/**
34+
* The permission type for the authentication.
35+
*/
36+
type: string;
37+
38+
/**
39+
* Indicates whether the access key is currently useable.
40+
*/
41+
isActive: boolean;
42+
43+
/**
44+
* An optional comment or description that provides additional context for the access key.
45+
*/
46+
comment?: string;
47+
48+
/**
49+
* An optional expiration timestamp (in Unix time) indicating when the access key expires.
50+
* If `null`, the key does not expire.
51+
*/
52+
expiration?: string | null;
53+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*!
2+
* This file is part of *** M y C o R e ***
3+
* See https://www.mycore.de/ for details.
4+
*
5+
* MyCoRe is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* MyCoRe is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
/**
20+
* DTO for partially updating an access key.
21+
*/
22+
export interface PartialUpdateAccessKeyDto {
23+
/**
24+
* An optional reference or name associated with the access key.
25+
*/
26+
reference?: string;
27+
28+
/**
29+
* An optional permission type for the authentication.
30+
*/
31+
type?: string;
32+
33+
/**
34+
* An optional that indicates whether the access key is currently useable.
35+
*/
36+
isActive?: boolean;
37+
38+
/**
39+
* An optional comment or description that provides additional context for the access key.
40+
*/
41+
comment?: string;
42+
43+
/**
44+
* An optional expiration timestamp (in Unix time) indicating when the access key expires.
45+
* If `null`, the key does not expire.
46+
*/
47+
expiration?: string | null;
48+
}

0 commit comments

Comments
 (0)