diff --git a/example/a.ts b/example/a.ts index 49bd35d6..bb01b2e7 100644 --- a/example/a.ts +++ b/example/a.ts @@ -1,52 +1,20 @@ import { Elysia, t } from '../src' -const app = new Elysia() - .onParse('custom', ({ contentType, request }) => { - if (contentType.startsWith('application/x-elysia-1')) - return { name: 'Eden' } - }) - .onParse('custom2', ({ contentType, request }) => { - if (contentType.startsWith('application/x-elysia-2')) - return { name: 'Pardofelis' } - }) - .post('/json', ({ body }) => body, { - parse: ['custom'] - }) +export const auth = new Elysia().macro({ + isAuth(isAuth: boolean) { + return { + resolve() { + return { + user: 'saltyaom' + } + } + } + }, + role(role: 'admin' | 'user') { + return {} + } +}) -const response = await Promise.all([ - app - .handle( - new Request('http://localhost:3000/json', { - method: 'POST', - headers: { - 'content-type': 'application/json' - }, - body: JSON.stringify({ name: 'Aru' }) - }) - ) - .then((x) => x.json()), - app - .handle( - new Request('http://localhost:3000/json', { - method: 'POST', - headers: { - 'content-type': 'application/x-elysia-1' - }, - body: JSON.stringify({ name: 'Aru' }) - }) - ) - .then((x) => x.text()), - app - .handle( - new Request('http://localhost:3000/json', { - method: 'POST', - headers: { - 'content-type': 'application/x-elysia-2' - }, - body: JSON.stringify({ name: 'Aru' }) - }) - ) - .then((x) => x.text()) -]) - -console.log(response) +new Elysia().ws('/ws', { + ping: (message) => message +}) diff --git a/package.json b/package.json index 9f39f744..0a195152 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "elysia", "description": "Ergonomic Framework for Human", - "version": "1.2.0-rc.0", + "version": "1.2.0", "author": { "name": "saltyAom", "url": "https://github.com/SaltyAom", @@ -118,6 +118,11 @@ "import": "./dist/universal/server.mjs", "require": "./dist/cjs/universal/server.js" }, + "./universal/env": { + "types": "./dist/universal/env.d.ts", + "import": "./dist/universal/env.mjs", + "require": "./dist/cjs/universal/env.js" + }, "./universal/file": { "types": "./dist/universal/file.d.ts", "import": "./dist/universal/file.mjs", @@ -148,8 +153,8 @@ "release": "npm run build && npm run test && npm publish" }, "dependencies": { - "@sinclair/typebox": "^0.34.7", - "cookie": "^1.0.1", + "@sinclair/typebox": "^0.34.13", + "cookie": "^1.0.2", "fast-decode-uri-component": "^1.0.1", "memoirist": "^0.2.0", "openapi-types": "^12.1.3" @@ -166,8 +171,8 @@ "eslint-plugin-sonarjs": "^0.23.0", "expect-type": "^0.16.0", "prettier": "^3.3.3", - "tsup": "^8.3.0", - "typescript": "^5.5.2" + "tsup": "^8.3.5", + "typescript": "^5.7.2" }, "peerDependencies": { "@sinclair/typebox": ">= 0.34.0", diff --git a/src/index.ts b/src/index.ts index d15540a1..21fbfc5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,7 @@ import { BunAdapter } from './adapter/bun/index' import { WebStandardAdapter } from './adapter/web-standard/index' import type { ElysiaAdapter } from './adapter/types' +import { env } from './universal/env' import type { ListenCallback, Serve, Server } from './universal/server' import { @@ -348,15 +349,15 @@ export default class Elysia< '~adapter': ElysiaAdapter - env(model: TObject, env = Bun?.env ?? process.env) { + env(model: TObject, _env = env) { const validator = getSchemaValidator(model, { dynamic: true, additionalProperties: true, coerce: true }) - if (validator.Check(env) === false) { - const error = new ValidationError('env', model, env) + if (validator.Check(_env) === false) { + const error = new ValidationError('env', model, _env) throw new Error(error.all.map((x) => x.summary).join('\n')) } @@ -429,7 +430,7 @@ export default class Elysia< applyConfig(config: ElysiaConfig) { this.config = { prefix: '', - aot: process.env.ELYSIA_AOT !== 'false', + aot: env.ELYSIA_AOT !== 'false', normalize: true, ...config, cookie: { @@ -6168,6 +6169,7 @@ export type { CoExist } from './types' +export { env } from './universal/env' export { file, ElysiaFile } from './universal/file' export type { ElysiaAdapter } from './adapter' diff --git a/src/universal/env.ts b/src/universal/env.ts new file mode 100644 index 00000000..187bbbb3 --- /dev/null +++ b/src/universal/env.ts @@ -0,0 +1,7 @@ +import { isBun } from './utils' + +export const env = isBun + ? Bun.env + : typeof process !== 'undefined' && process?.env + ? process.env + : {} diff --git a/src/universal/index.ts b/src/universal/index.ts index 2adca603..4b5e4fce 100644 --- a/src/universal/index.ts +++ b/src/universal/index.ts @@ -1,3 +1,4 @@ +export { env } from './env' export { file } from './file' export type { ErrorLike,