@krak-stack/auth 0.1.25 → 0.1.28

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.
package/dist/client.js DELETED
@@ -1,105 +0,0 @@
1
- import { Cache, Duration, Context, Effect, Layer, Redacted, Exit, } from "effect";
2
- import { FetchHttpClient, HttpClient, HttpClientRequest, } from "effect/unstable/http";
3
- import { HttpApiClient } from "effect/unstable/httpapi";
4
- import { BetterAuthApi } from "./better-auth/api";
5
- import { AuthClientConfig } from "./config";
6
- import { ExtraApi } from "./extra/api";
7
- import { ServerApi } from "./server/api";
8
- const forwardedAuthHeaderNames = [
9
- "accept-language",
10
- "cookie",
11
- "user-agent",
12
- ];
13
- const forwardedAuthHeaders = (headers) => {
14
- const forwarded = {};
15
- for (const name of forwardedAuthHeaderNames) {
16
- const value = headers[name];
17
- if (value)
18
- forwarded[name] = value;
19
- }
20
- return forwarded;
21
- };
22
- const authHttpClientLayer = (headers) => Layer.effect(HttpClient.HttpClient, Effect.gen(function* () {
23
- const http = yield* HttpClient.HttpClient;
24
- return HttpClient.mapRequest(http, (request) => HttpClientRequest.setHeaders(request, forwardedAuthHeaders(headers)));
25
- })).pipe(Layer.provide(FetchHttpClient.layer));
26
- const getSessionCacheKey = (headers) => JSON.stringify(forwardedAuthHeaders(headers));
27
- const clientConfigOptions = ({ baseUrl, apiKey }) => ({
28
- ...(baseUrl ? { baseUrl } : {}),
29
- ...(apiKey ? { apiKey } : {}),
30
- });
31
- export class AuthApiClients extends Context.Service()("@krak-stack/auth/AuthApiClients", {
32
- make: Effect.gen(function* () {
33
- const config = yield* AuthClientConfig;
34
- const http = yield* HttpClient.HttpClient;
35
- const httpClient = HttpClient.mapRequest(http, (request) => HttpClientRequest.bearerToken(request, Redacted.value(config.apiKey)));
36
- const serverUsers = yield* HttpApiClient.group(ServerApi, {
37
- group: "serverUsers",
38
- baseUrl: config.baseUrl,
39
- httpClient,
40
- });
41
- const serverOrganizations = yield* HttpApiClient.group(ServerApi, {
42
- group: "serverOrganizations",
43
- baseUrl: config.baseUrl,
44
- httpClient,
45
- });
46
- const serverDomains = yield* HttpApiClient.group(ServerApi, {
47
- group: "serverDomains",
48
- baseUrl: config.baseUrl,
49
- httpClient,
50
- });
51
- const extra = yield* HttpApiClient.group(ExtraApi, {
52
- group: "extra",
53
- baseUrl: config.baseUrl,
54
- httpClient,
55
- });
56
- const betterAuth = yield* HttpApiClient.group(BetterAuthApi, {
57
- group: "betterAuth",
58
- baseUrl: config.baseUrl,
59
- httpClient: http,
60
- });
61
- return {
62
- ...betterAuth,
63
- server: {
64
- ...serverUsers,
65
- ...serverOrganizations,
66
- domains: {
67
- create: serverDomains.createDomain,
68
- get: serverDomains.getDomain,
69
- getByHost: serverDomains.getDomainByHost,
70
- records: serverDomains.getDomainRecords,
71
- delete: serverDomains.deleteDomain,
72
- },
73
- },
74
- extra,
75
- };
76
- }),
77
- }) {
78
- static layer = (options = {}) => {
79
- const headers = options.headers ?? {};
80
- return Layer.effect(this, this.make).pipe(Layer.provide(AuthClientConfig.layer(clientConfigOptions(options))), Layer.provide(authHttpClientLayer(headers)));
81
- };
82
- }
83
- const sessionCache = Effect.runSync(Cache.makeWith(() => Effect.gen(function* () {
84
- const auth = yield* AuthApiClients;
85
- return yield* auth.getSession();
86
- }), {
87
- capacity: 500,
88
- timeToLive: (exit) => Exit.isSuccess(exit) ? Duration.seconds(2) : Duration.zero,
89
- requireServicesAt: "lookup",
90
- }));
91
- export class AuthClient extends Context.Service()("@krak-stack/auth/AuthClient", {
92
- make: (headers = {}) => Effect.gen(function* () {
93
- const clients = yield* AuthApiClients;
94
- const sessionCacheKey = getSessionCacheKey(headers);
95
- return {
96
- ...clients,
97
- getSession: () => Cache.get(sessionCache, sessionCacheKey).pipe(Effect.provideService(AuthApiClients, clients)),
98
- };
99
- }),
100
- }) {
101
- static layer = (options = {}) => {
102
- const headers = options.headers ?? {};
103
- return Layer.effect(this, this.make(headers)).pipe(Layer.provide(AuthClientConfig.layer(clientConfigOptions(options))), Layer.provide(AuthApiClients.layer(options)));
104
- };
105
- }