@krak-stack/auth 0.1.25 → 0.1.27

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,67 @@
1
+ import { Cache, Context, Duration, Effect, Exit, Layer, Redacted, } from "effect";
2
+ import { FetchHttpClient, HttpClient, HttpClientRequest, } from "effect/unstable/http";
3
+ import { HttpApiClient } from "effect/unstable/httpapi";
4
+ import { AuthApi } from "./api";
5
+ import { AuthClientConfig } from "./config";
6
+ const forwardedAuthHeaderNames = [
7
+ "accept-language",
8
+ "cookie",
9
+ "user-agent",
10
+ ];
11
+ const forwardedAuthHeaders = (headers) => {
12
+ const forwarded = {};
13
+ for (const name of forwardedAuthHeaderNames) {
14
+ const value = headers[name];
15
+ if (value)
16
+ forwarded[name] = value;
17
+ }
18
+ return forwarded;
19
+ };
20
+ const authHttpClientLayer = (headers) => Layer.effect(HttpClient.HttpClient, Effect.gen(function* () {
21
+ const http = yield* HttpClient.HttpClient;
22
+ return HttpClient.mapRequest(http, (request) => HttpClientRequest.setHeaders(request, forwardedAuthHeaders(headers)));
23
+ })).pipe(Layer.provide(FetchHttpClient.layer));
24
+ const getSessionCacheKey = (headers) => JSON.stringify(forwardedAuthHeaders(headers));
25
+ const clientConfigOptions = ({ baseUrl, apiKey }) => ({
26
+ ...(baseUrl ? { baseUrl } : {}),
27
+ ...(apiKey ? { apiKey } : {}),
28
+ });
29
+ const isBetterAuthSessionRequest = (request) => request.url.endsWith("/api/auth/get-session");
30
+ class SessionLookup extends Context.Service()("@krak-stack/auth/SessionLookup") {
31
+ }
32
+ const sessionCache = Effect.runSync(Cache.makeWith(() => Effect.gen(function* () {
33
+ const lookup = yield* SessionLookup;
34
+ return yield* lookup.getSession();
35
+ }), {
36
+ capacity: 500,
37
+ timeToLive: (exit) => Exit.isSuccess(exit) ? Duration.seconds(2) : Duration.zero,
38
+ requireServicesAt: "lookup",
39
+ }));
40
+ export class AuthService extends Context.Service()("@krak-stack/auth/AuthService", {
41
+ make: (headers = {}) => Effect.gen(function* () {
42
+ const config = yield* AuthClientConfig;
43
+ const http = yield* HttpClient.HttpClient;
44
+ const httpClient = HttpClient.mapRequest(http, (request) => isBetterAuthSessionRequest(request)
45
+ ? request
46
+ : HttpClientRequest.bearerToken(request, Redacted.value(config.apiKey)));
47
+ const api = yield* HttpApiClient.makeWith(AuthApi, {
48
+ baseUrl: config.baseUrl,
49
+ httpClient,
50
+ });
51
+ const sessionCacheKey = getSessionCacheKey(headers);
52
+ return {
53
+ ...api,
54
+ auth: {
55
+ ...api.auth,
56
+ getSession: () => Cache.get(sessionCache, sessionCacheKey).pipe(Effect.provideService(SessionLookup, {
57
+ getSession: api.auth.getSession,
58
+ })),
59
+ },
60
+ };
61
+ }),
62
+ }) {
63
+ static layer = (options = {}) => {
64
+ const headers = options.headers ?? {};
65
+ return Layer.effect(this, this.make(headers)).pipe(Layer.provide(AuthClientConfig.layer(clientConfigOptions(options))), Layer.provide(authHttpClientLayer(headers)));
66
+ };
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krak-stack/auth",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "description": "Effect HttpApi clients and schemas for KrakStack Auth.",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -13,6 +13,14 @@
13
13
  "types": "./dist/index.d.ts",
14
14
  "default": "./dist/index.js"
15
15
  },
16
+ "./api": {
17
+ "types": "./dist/api.d.ts",
18
+ "default": "./dist/api.js"
19
+ },
20
+ "./service": {
21
+ "types": "./dist/service.d.ts",
22
+ "default": "./dist/service.js"
23
+ },
16
24
  "./schema": {
17
25
  "types": "./dist/schema.d.ts",
18
26
  "default": "./dist/schema.js"
@@ -39,6 +47,8 @@
39
47
  },
40
48
  "scripts": {
41
49
  "build": "rm -rf dist && tsc -p tsconfig.json",
50
+ "build:watch": "rm -rf dist && tsc -p tsconfig.json --watch --preserveWatchOutput",
51
+ "dev": "bun run build:watch",
42
52
  "prepack": "bun run build",
43
53
  "type:check": "tsc -p tsconfig.json --noEmit"
44
54
  },