keycloak-api 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+
2
+ 
3
+ > keycloak-api@0.0.0 build /Users/sfera/Workspace/xata/extra/openapi-clients/packages/keycloak-api
4
+ > rimraf dist && tsup
5
+
6
+ ✘ [ERROR] Expected identifier but found "import"
7
+
8
+ (define name):1:0:
9
+  1 │ import.meta.url
10
+ ╵ ~~~~~~
11
+
12
+ Error: Build failed with 1 error:
13
+ (define name):1:0: ERROR: Expected identifier but found "import"
14
+ at failureErrorWithLog (/Users/sfera/Workspace/xata/extra/openapi-clients/node_modules/.pnpm/esbuild@0.24.1/node_modules/esbuild/lib/main.js:1476:15)
15
+ at /Users/sfera/Workspace/xata/extra/openapi-clients/node_modules/.pnpm/esbuild@0.24.1/node_modules/esbuild/lib/main.js:945:25
16
+ at runOnEndCallbacks (/Users/sfera/Workspace/xata/extra/openapi-clients/node_modules/.pnpm/esbuild@0.24.1/node_modules/esbuild/lib/main.js:1316:45)
17
+ at buildResponseToResult (/Users/sfera/Workspace/xata/extra/openapi-clients/node_modules/.pnpm/esbuild@0.24.1/node_modules/esbuild/lib/main.js:943:7)
18
+ at /Users/sfera/Workspace/xata/extra/openapi-clients/node_modules/.pnpm/esbuild@0.24.1/node_modules/esbuild/lib/main.js:970:16
19
+ at responseCallbacks.<computed> (/Users/sfera/Workspace/xata/extra/openapi-clients/node_modules/.pnpm/esbuild@0.24.1/node_modules/esbuild/lib/main.js:622:9)
20
+ at handleIncomingPacket (/Users/sfera/Workspace/xata/extra/openapi-clients/node_modules/.pnpm/esbuild@0.24.1/node_modules/esbuild/lib/main.js:677:12)
21
+ at Socket.readFromStdout (/Users/sfera/Workspace/xata/extra/openapi-clients/node_modules/.pnpm/esbuild@0.24.1/node_modules/esbuild/lib/main.js:600:7)
22
+ at Socket.emit (node:events:524:28)
23
+ at addChunk (node:internal/streams/readable:561:12)
24
+  ELIFECYCLE  Command failed with exit code 1.
package/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # Keycloak API JavaScript SDK
2
+
3
+ Unofficial Keycloak API JavaScript SDK built from the OpenAPI specification and with TypeScript types.
4
+
5
+ ```ts
6
+ const keycloak = new KeycloakApi({ token: 'token' });
7
+ const realms = await keycloak.request('GET /admin/realms', {});
8
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,88 @@
1
+ 'use strict';
2
+
3
+ function notEmpty(value) {
4
+ return value !== null && value !== void 0;
5
+ }
6
+ function compactObject(obj) {
7
+ return Object.fromEntries(Object.entries(obj).filter(([, value]) => notEmpty(value)));
8
+ }
9
+
10
+ async function fetch$1({
11
+ url,
12
+ method,
13
+ body,
14
+ headers,
15
+ pathParams,
16
+ queryParams,
17
+ signal,
18
+ baseUrl,
19
+ token,
20
+ fetchImpl
21
+ }) {
22
+ try {
23
+ const requestHeaders = compactObject({
24
+ "Content-Type": "application/json",
25
+ Authorization: token ? `Bearer ${token}` : void 0,
26
+ ...headers
27
+ });
28
+ if (requestHeaders["Content-Type"]?.toLowerCase().includes("multipart/form-data")) {
29
+ delete requestHeaders["Content-Type"];
30
+ }
31
+ const payload = body instanceof FormData ? body : requestHeaders["Content-Type"] === "application/json" ? JSON.stringify(body) : body;
32
+ const response = await fetchImpl(`${baseUrl}${resolveUrl(url, queryParams, pathParams)}`, {
33
+ signal,
34
+ method: method.toUpperCase(),
35
+ body: payload,
36
+ headers: requestHeaders
37
+ });
38
+ if (!response.ok) {
39
+ let error;
40
+ try {
41
+ error = await response.json();
42
+ } catch (e) {
43
+ error = {
44
+ status: "unknown",
45
+ payload: e instanceof Error ? `Unexpected error (${e.message})` : "Unexpected error"
46
+ };
47
+ }
48
+ throw error;
49
+ }
50
+ if (response.headers?.get("content-type")?.includes("json")) {
51
+ return await response.json();
52
+ } else {
53
+ return await response.text();
54
+ }
55
+ } catch (e) {
56
+ let errorObject = {
57
+ name: "unknown",
58
+ message: e instanceof Error ? `Network error (${e.message})` : "Network error",
59
+ stack: e
60
+ };
61
+ throw errorObject;
62
+ }
63
+ }
64
+ const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
65
+ let query = new URLSearchParams(queryParams).toString();
66
+ if (query) query = `?${query}`;
67
+ return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)] ?? "") + query;
68
+ };
69
+
70
+ class KeycloakApi {
71
+ #baseUrl;
72
+ #token;
73
+ #fetch;
74
+ constructor(options) {
75
+ this.#baseUrl = options.baseUrl;
76
+ this.#token = options.token;
77
+ this.#fetch = options.fetch || fetch;
78
+ if (!this.#fetch) throw new Error("Fetch is required");
79
+ }
80
+ async request(endpoint, params) {
81
+ const [method = "", url = ""] = endpoint.split(" ");
82
+ const extraParams = params || {};
83
+ const result = await fetch$1({ ...extraParams, method, url, baseUrl: this.#baseUrl, token: this.#token, fetchImpl: this.#fetch });
84
+ return result;
85
+ }
86
+ }
87
+
88
+ exports.KeycloakApi = KeycloakApi;