CAKE20

AI Web PaaS

Shared and automatic import

The code and types used by UI, API, jobs, and tasks are kept in one place.

common functions

Values ​​declared with named export are imported from Cake20 View pages, APIs, jobs, and tasks. You can use it without it. If the same name is exported from two files, This will stop generating automatic imports.

// shared/utils/price.ts
export function formatPrice(value: number) {
  return value.toLocaleString("ko-KR") + "one";
}

common type

// shared/types/user.d.ts
export interface UserSummary {
  id: number;
  name: string;
}

Automatic import for screen and server only

Named exports in app/types and app/utils are automatically imported only in screen code.

Named exports of server/types and server/utils include API, route, hook, and task. Automatic import only from server code including seed.

Named exports of shared/types and shared/utils are used on both the screen and the server. Global automatic import.

Even if there are no unused shared, server/types and server/utils folders Build, no files are required to maintain an empty selection folder.

You cannot import a dedicated folder from the opposite area or shared. on both sides Place necessary declarations in shared/types or shared/utils.

// app/utils/money.ts: UI only
export const money = (value: number) => value.toLocaleString("ko-KR");

// server/types/session.d.ts: Only used on servers
export interface SessionData { userId: number }

~ path alias

~/ in the screen code is the app folder of the website, and ~/ in the server code is Use the server folder as the top-level path.

shared/types and shared/utils are global auto-imports, so ~/ or relative It doesn't import it again by path.

The section that goes outside the app and server is at the top of the current execution area. Invalidate. Static imports that point to shared are removed when saving. Dynamic imports that require a module object will lead to an error.

// app/pages/about.tsx
import { browserLang } from "~/lib/browser";

// Screen code: ~/lib/browser → app/lib/browser
// Server code: ~/path → server/path

Runtime common features

fetchGet, fetchPost, now, toDate, textDate and startOfToday are used for UI and Use it on the server without import. Date's textFull, addDate etc. The runtime also installs extended properties and methods.

DB connection and Prisma Client interface for each website are automatically created in the DB model. is created. Do not redeclare these functions in shared/utils.

shared leaves only the website's unique code. If you copy the Runtime built-in functions, Version conflicts and auto-import name conflicts may occur.

Share your login status

  • Just use auth.login, auth.user, auth.require and auth.logout.
  • Internal session storage and cookie security settings are handled by Cake20.
  • Secure cookies are automatically applied on production domains.
// server/api/login.post.ts
export const config = {
  filter: { userId: z.number() },
  sample: { userId: 1 }
};

export default async (data: Input<typeof config.filter>) => {
  const login = await auth.login({ id: data.userId });
  return Response.json({ ok: true }, {
    headers: { "set-cookie": login.cookie }
  });
};

// server/api/me.get.ts
export default async (_input: unknown, context: ApiContext) => ({
  user: await auth.user(context.request)
});