@cubicecho/server-kit 0.1.0 → 0.3.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.
Files changed (60) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +0 -0
  3. package/_agents/docs/01-database.md +0 -0
  4. package/_agents/docs/02-columns.md +0 -0
  5. package/_agents/docs/03-testing.md +0 -0
  6. package/_agents/docs/04-auth.md +54 -0
  7. package/_agents/docs/05-graphql.md +24 -0
  8. package/_agents/docs/common-mistakes.md +0 -0
  9. package/dist/auth/defaults.d.ts +31 -0
  10. package/dist/auth/defaults.js +18 -0
  11. package/dist/auth/index.d.ts +4 -0
  12. package/dist/auth/index.js +4 -0
  13. package/dist/auth/plugins.d.ts +1270 -0
  14. package/dist/auth/plugins.js +39 -0
  15. package/dist/auth/preset.d.ts +53 -0
  16. package/dist/auth/preset.js +21 -0
  17. package/dist/auth/rate-limit.d.ts +32 -0
  18. package/dist/auth/rate-limit.js +60 -0
  19. package/dist/columns/index.d.ts +0 -0
  20. package/dist/columns/index.js +0 -0
  21. package/dist/consts.d.ts +0 -0
  22. package/dist/consts.js +0 -0
  23. package/dist/db/connection.d.ts +0 -0
  24. package/dist/db/connection.js +0 -0
  25. package/dist/db/create.d.ts +0 -0
  26. package/dist/db/create.js +0 -0
  27. package/dist/db/defaults.d.ts +0 -0
  28. package/dist/db/defaults.js +0 -0
  29. package/dist/db/errors.d.ts +0 -0
  30. package/dist/db/errors.js +0 -0
  31. package/dist/db/index.d.ts +0 -0
  32. package/dist/db/index.js +0 -0
  33. package/dist/db/prepare.d.ts +0 -0
  34. package/dist/db/prepare.js +0 -0
  35. package/dist/db/ssl.d.ts +0 -0
  36. package/dist/db/ssl.js +0 -0
  37. package/dist/db/version.d.ts +0 -0
  38. package/dist/db/version.js +0 -0
  39. package/dist/db/wait.d.ts +0 -0
  40. package/dist/db/wait.js +0 -0
  41. package/dist/graphql/defaults.d.ts +17 -0
  42. package/dist/graphql/defaults.js +10 -0
  43. package/dist/graphql/errors.d.ts +59 -0
  44. package/dist/graphql/errors.js +82 -0
  45. package/dist/graphql/index.d.ts +5 -0
  46. package/dist/graphql/index.js +5 -0
  47. package/dist/graphql/logger.d.ts +13 -0
  48. package/dist/graphql/logger.js +38 -0
  49. package/dist/graphql/operation-limits.d.ts +25 -0
  50. package/dist/graphql/operation-limits.js +88 -0
  51. package/dist/graphql/validation.d.ts +28 -0
  52. package/dist/graphql/validation.js +19 -0
  53. package/dist/testing/index.d.ts +0 -0
  54. package/dist/testing/index.js +0 -0
  55. package/dist/testing/postgres-problems.d.ts +0 -0
  56. package/dist/testing/postgres-problems.js +0 -0
  57. package/dist/testing/schema-problems.d.ts +0 -0
  58. package/dist/testing/schema-problems.js +0 -0
  59. package/llms.txt +45 -1
  60. package/package.json +27 -3
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,54 @@
1
+ # Auth preset
2
+
3
+ `@cubicecho/server-kit/auth` has the better-auth options and sign-in plugins every server shares: sessions in Postgres, bearer tokens, emailed links, and OIDC with pairing. The app still calls `betterAuth`, so it adds its own plugins and hooks and keeps exact types.
4
+
5
+ ```ts
6
+ import { apiKey } from '@better-auth/api-key';
7
+ import { authPreset, signInPlugins } from '@cubicecho/server-kit/auth';
8
+ import { betterAuth } from 'better-auth';
9
+
10
+ const auth = betterAuth({
11
+ ...authPreset({
12
+ db,
13
+ tables: {
14
+ user: users,
15
+ session: sessions,
16
+ account: accounts,
17
+ verification: verifications,
18
+ apikey: apikeys,
19
+ ssoProvider: ssoProviders,
20
+ },
21
+ secret,
22
+ serverUrl,
23
+ appUrl,
24
+ }),
25
+ databaseHooks: { user: { create: { after: (user) => claimFirstAdmin(db, user.id) } } },
26
+ plugins: [
27
+ apiKey({ defaultPrefix: 'todo_', requireName: true, rateLimit: { enabled: false } }),
28
+ ...signInPlugins({
29
+ appUrl,
30
+ deliver: sendMagicLinkEmail,
31
+ oidc: { clientName: 'Todo', canPair: ({ userId }) => mayPair(userId) },
32
+ }),
33
+ ],
34
+ });
35
+ ```
36
+
37
+ - Set every `id` column with `id()`: the preset leaves ids to the database.
38
+ - `deliver: undefined` is an instance that cannot email. `sendsMagicLinks(auth)` says which it is.
39
+ - `paths` moves the app's `login`, `verify` and `signedIn` pages.
40
+ - An option after the spread replaces the preset's whole value, so `session: { … }` has to repeat `expiresIn`.
41
+ - `@better-auth/sso` and `better-auth` are peers: install both in the server.
42
+
43
+ ## Rate limiting sign-in
44
+
45
+ better-auth's own limiter never sees a server-side call, so every sign-in step calls `throttle` first.
46
+
47
+ ```ts
48
+ import { createRateLimiter, throttle } from '@cubicecho/server-kit/auth';
49
+
50
+ const limiter = createRateLimiter();
51
+ throttle({ limiter, ip: request.ip }, 'sign-in-magic-link', email);
52
+ ```
53
+
54
+ It counts by address and by account, each step on its own budget, and throws `TOO_MANY_REQUESTS` with `retryAfter`.
@@ -0,0 +1,24 @@
1
+ # GraphQL
2
+
3
+ `@cubicecho/server-kit/graphql` has what every Yoga 6 server shares: operation limits, coded errors, input validation and a logger that never prints a request's variables.
4
+
5
+ ```ts
6
+ import { graphqlLogger, REQUEST_ID, useOperationLimits } from '@cubicecho/server-kit/graphql';
7
+ import { createYoga } from 'graphql-yoga';
8
+
9
+ const yoga = createYoga({
10
+ schema,
11
+ graphqlEndpoint: '/graphql',
12
+ logging: graphqlLogger,
13
+ requestId: REQUEST_ID,
14
+ plugins: [useOperationLimits()],
15
+ });
16
+ ```
17
+
18
+ - `useOperationLimits({ maxCost: 15_000 })` replaces a bound of `OPERATION_LIMIT_DEFAULTS`. It holds subscriptions to them too.
19
+ - Give `buildSchema` the same page sizes: `limits: { defaultLimit: OPERATION_LIMIT_DEFAULTS.defaultPageSize, maxLimit: OPERATION_LIMIT_DEFAULTS.maxPageSize }`.
20
+ - `badInput`, `notFound`, `forbidden`, `conflict`, `unauthenticated` and `rateLimited` build the errors a client reads, by `ErrorCode`. Anything else thrown is masked and logged.
21
+ - `parseOrThrow(schema, value)` takes a zod 4 schema and throws `BAD_USER_INPUT` with its first issue.
22
+ - `requireAuth(context)` returns `context.userId`, or throws `UNAUTHENTICATED`.
23
+ - `REQUEST_ID` gives each request an id of the server's own. It is in the log line of a masked error and in the `x-request-id` response header.
24
+ - In tests, alias `graphql` to `node_modules/graphql/index.js`: Vitest otherwise loads its two builds.
File without changes
@@ -0,0 +1,31 @@
1
+ /** Session and link lifetimes. */
2
+ export interface AuthSettings {
3
+ /** How long a session lasts after it was last used. Long: there is no password to fall back on. */
4
+ sessionTtlDays: number;
5
+ /** How long an emailed sign-in link works. Short: it travels by mail. */
6
+ magicLinkTtlMinutes: number;
7
+ }
8
+ /** What `authPreset` and `signInPlugins` use when the app overrides nothing. */
9
+ export declare const AUTH_DEFAULTS: Readonly<AuthSettings>;
10
+ /** The app's pages a sign-in passes through, as paths under `appUrl`. */
11
+ export interface AuthPaths {
12
+ /** The sign-in page, where a failed sign-in goes. */
13
+ login: string;
14
+ /** Spends an emailed link's token. */
15
+ verify: string;
16
+ /** Spends the one-time token an OIDC sign-in ends in. */
17
+ signedIn: string;
18
+ }
19
+ /** Where those pages are when the app overrides nothing. */
20
+ export declare const AUTH_PATHS: Readonly<AuthPaths>;
21
+ /** The sign-in throttle. */
22
+ export interface RateLimitSettings {
23
+ /** Attempts allowed per key inside one window. */
24
+ maxAttempts: number;
25
+ /** How long an attempt counts against its key. */
26
+ windowMinutes: number;
27
+ /** Past this many keys, a hit sweeps out the ones whose window has passed. */
28
+ sweepAtKeys: number;
29
+ }
30
+ /** What `createRateLimiter` uses when the app overrides nothing. */
31
+ export declare const RATE_LIMIT_DEFAULTS: Readonly<RateLimitSettings>;
@@ -0,0 +1,18 @@
1
+ // Every value someone might tune, as plain data. Nothing here computes, reads the environment or imports.
2
+ /** What `authPreset` and `signInPlugins` use when the app overrides nothing. */
3
+ export const AUTH_DEFAULTS = Object.freeze({
4
+ sessionTtlDays: 30,
5
+ magicLinkTtlMinutes: 15,
6
+ });
7
+ /** Where those pages are when the app overrides nothing. */
8
+ export const AUTH_PATHS = Object.freeze({
9
+ login: '/login',
10
+ verify: '/auth/verify',
11
+ signedIn: '/auth/sso',
12
+ });
13
+ /** What `createRateLimiter` uses when the app overrides nothing. */
14
+ export const RATE_LIMIT_DEFAULTS = Object.freeze({
15
+ maxAttempts: 5,
16
+ windowMinutes: 15,
17
+ sweepAtKeys: 10_000,
18
+ });
@@ -0,0 +1,4 @@
1
+ export { AUTH_DEFAULTS, AUTH_PATHS, type AuthPaths, type AuthSettings, RATE_LIMIT_DEFAULTS, type RateLimitSettings, } from './defaults.ts';
2
+ export { type MagicLink, type SignInOptions, sendsMagicLinks, signInPlugins } from './plugins.ts';
3
+ export { type AuthPresetOptions, type AuthTables, authPreset } from './preset.ts';
4
+ export { createRateLimiter, type RateLimiter, type ThrottleContext, throttle } from './rate-limit.ts';
@@ -0,0 +1,4 @@
1
+ export { AUTH_DEFAULTS, AUTH_PATHS, RATE_LIMIT_DEFAULTS, } from './defaults.js';
2
+ export { sendsMagicLinks, signInPlugins } from './plugins.js';
3
+ export { authPreset } from './preset.js';
4
+ export { createRateLimiter, throttle } from './rate-limit.js';