@elysiajs/eden 0.1.0-rc.2 → 0.1.0-rc.4

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/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import type { Elysia, SCHEMA } from 'elysia';
2
- import { CreateEden, UnionToIntersection } from './types';
3
- export declare function eden<App extends Elysia<any>>(domain: string): App['store'] extends {
4
- [key in typeof SCHEMA]: any;
5
- } ? UnionToIntersection<CreateEden<App['store'][typeof SCHEMA]>> : never;
1
+ import type { Elysia } from 'elysia';
2
+ import { Eden } from './types';
3
+ export declare const eden: <App extends Elysia<any>>(domain: string) => Eden<App>;
package/dist/index.js CHANGED
@@ -1,45 +1 @@
1
- const camelToDash = (str) => str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
2
- const composePath = (domain, path, query) => {
3
- if (!domain.endsWith('/'))
4
- domain += '/';
5
- path = camelToDash(path.replace(/index/g, ''));
6
- if (!query || !Object.keys(query).length)
7
- return `${domain}${path}`;
8
- let q = '';
9
- for (const [key, value] of Object.entries(query))
10
- q += `${key}=${value}&`;
11
- return `${domain}${path}?${q.slice(0, -1)}`;
12
- };
13
- const createProxy = (domain, path = '') => new Proxy(() => { }, {
14
- get(target, key, value) {
15
- return createProxy(domain, `${path}/${key.toString()}`);
16
- },
17
- apply(target, _, [{ $query, $fetch, ...body } = {
18
- $fetch: undefined,
19
- $query: undefined
20
- }] = [{}]) {
21
- const i = path.lastIndexOf('/');
22
- return fetch(composePath(domain, path.slice(0, i), $query), {
23
- method: path.slice(i + 1),
24
- headers: {
25
- 'content-type': 'application/json',
26
- ...$fetch?.['headers']
27
- },
28
- body: Object.keys(body).length
29
- ? JSON.stringify(body)
30
- : undefined,
31
- ...$fetch
32
- }).then((res) => {
33
- if (res.headers.get('content-type') === 'application/json')
34
- return res.json();
35
- return res.text();
36
- });
37
- }
38
- });
39
- export function eden(domain) {
40
- return new Proxy({}, {
41
- get(target, key, value) {
42
- return createProxy(domain, key);
43
- }
44
- });
45
- }
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=t=>t.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`),u=(t,e,n)=>{if(t.endsWith("/")||(t+="/"),e=g(e.replace(/index/g,"")),!n||!Object.keys(n).length)return`${t}${e}`;let r="";for(const[c,o]of Object.entries(n))r+=`${c}=${o}&`;return`${t}${e}?${r.slice(0,-1)}`},a=(t,e="")=>new Proxy(()=>{},{get(n,r,c){return a(t,`${e}/${r.toString()}`)},apply(n,r,[{$query:c,$fetch:o,...i}={$fetch:void 0,$query:void 0}]=[{}]){const l=e.lastIndexOf("/");return fetch(u(t,e.slice(0,l),c),{method:e.slice(l+1),headers:{"content-type":"application/json",...o==null?void 0:o.headers},body:Object.keys(i).length?JSON.stringify(i):void 0,...o}).then(async s=>{if(s.status>=300)throw new Error(await s.text());return s.headers.get("content-type")==="application/json"?s.json():s.text()})}}),y=t=>new Proxy({},{get(e,n,r){return a(t,n)}});exports.eden=y;
package/dist/index.mjs ADDED
@@ -0,0 +1,44 @@
1
+ const g = (t) => t.replace(/[A-Z]/g, (e) => `-${e.toLowerCase()}`), u = (t, e, n) => {
2
+ if (t.endsWith("/") || (t += "/"), e = g(e.replace(/index/g, "")), !n || !Object.keys(n).length)
3
+ return `${t}${e}`;
4
+ let r = "";
5
+ for (const [c, o] of Object.entries(n))
6
+ r += `${c}=${o}&`;
7
+ return `${t}${e}?${r.slice(0, -1)}`;
8
+ }, l = (t, e = "") => new Proxy(() => {
9
+ }, {
10
+ get(n, r, c) {
11
+ return l(t, `${e}/${r.toString()}`);
12
+ },
13
+ apply(n, r, [
14
+ { $query: c, $fetch: o, ...i } = {
15
+ $fetch: void 0,
16
+ $query: void 0
17
+ }
18
+ ] = [{}]) {
19
+ const a = e.lastIndexOf("/");
20
+ return fetch(u(t, e.slice(0, a), c), {
21
+ method: e.slice(a + 1),
22
+ headers: {
23
+ "content-type": "application/json",
24
+ ...o == null ? void 0 : o.headers
25
+ },
26
+ body: Object.keys(i).length ? JSON.stringify(i) : void 0,
27
+ ...o
28
+ }).then(async (s) => {
29
+ if (s.status >= 300)
30
+ throw new Error(await s.text());
31
+ return s.headers.get("content-type") === "application/json" ? s.json() : s.text();
32
+ });
33
+ }
34
+ }), y = (t) => new Proxy(
35
+ {},
36
+ {
37
+ get(e, n, r) {
38
+ return l(t, n);
39
+ }
40
+ }
41
+ );
42
+ export {
43
+ y as eden
44
+ };
@@ -0,0 +1 @@
1
+ (function(o,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(o=typeof globalThis<"u"?globalThis:o||self,s(o["@elysia/eden"]={}))})(this,function(o){"use strict";const s=t=>t.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`),f=(t,e,n)=>{if(t.endsWith("/")||(t+="/"),e=s(e.replace(/index/g,"")),!n||!Object.keys(n).length)return`${t}${e}`;let r="";for(const[l,i]of Object.entries(n))r+=`${l}=${i}&`;return`${t}${e}?${r.slice(0,-1)}`},d=(t,e="")=>new Proxy(()=>{},{get(n,r,l){return d(t,`${e}/${r.toString()}`)},apply(n,r,[{$query:l,$fetch:i,...u}={$fetch:void 0,$query:void 0}]=[{}]){const a=e.lastIndexOf("/");return fetch(f(t,e.slice(0,a),l),{method:e.slice(a+1),headers:{"content-type":"application/json",...i==null?void 0:i.headers},body:Object.keys(u).length?JSON.stringify(u):void 0,...i}).then(async c=>{if(c.status>=300)throw new Error(await c.text());return c.headers.get("content-type")==="application/json"?c.json():c.text()})}}),y=t=>new Proxy({},{get(e,n,r){return d(t,n)}});o.eden=y,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
package/dist/types.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  /// <reference types="bun-types" />
2
- import type { TypedRoute } from 'elysia';
2
+ import type { Elysia, SCHEMA, TypedRoute } from 'elysia';
3
+ export declare type Eden<App extends Elysia<any>> = App['store'] extends {
4
+ [key in typeof SCHEMA]: any;
5
+ } ? UnionToIntersection<CreateEden<App['store'][typeof SCHEMA]>> : never;
3
6
  export interface EdenCall {
4
7
  [x: string]: any;
5
8
  $fetch?: RequestInit;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elysiajs/eden",
3
- "version": "0.1.0-rc.2",
3
+ "version": "0.1.0-rc.4",
4
4
  "description": "Fully type-safe Elysia client",
5
5
  "author": {
6
6
  "name": "saltyAom",
@@ -8,9 +8,10 @@
8
8
  "email": "saltyaom@gmail.com"
9
9
  },
10
10
  "main": "./dist/index.js",
11
+ "module": "./dist/index.mjs",
11
12
  "exports": {
12
- "require": "./dist/cjs/index.js",
13
- "import": "./dist/index.js",
13
+ "require": "./dist/index.js",
14
+ "import": "./dist/index.mjs",
14
15
  "node": "./dist/index.js",
15
16
  "default": "./dist/index.js"
16
17
  },
@@ -30,22 +31,22 @@
30
31
  "scripts": {
31
32
  "dev": "bun run --hot example/index.ts",
32
33
  "test": "bun wiptest",
33
- "build": "rimraf dist && npm run build:cjs && npm run build:esm",
34
- "build:cjs": "tsc --project tsconfig.cjs.json",
35
- "build:esm": "tsc --project tsconfig.esm.json",
34
+ "build": "vite build",
36
35
  "release": "npm run build && npm run test && npm publish --access public"
37
36
  },
38
37
  "peerDependencies": {
39
- "elysia": ">= 0.1.0-rc.4"
38
+ "elysia": ">= 0.1.0-rc.5"
40
39
  },
41
40
  "devDependencies": {
42
- "@elysiajs/cors": "^0.1.0-rc.2",
41
+ "@elysiajs/cors": "^0.1.0-rc.3",
43
42
  "@sinclair/typebox": "^0.25.13",
44
43
  "@types/node": "^18.11.7",
45
- "bun-types": "^0.2.2",
46
- "elysia": "^0.1.0-rc.4",
44
+ "bun-types": "^0.3.0",
45
+ "elysia": "^0.1.0-rc.5",
47
46
  "eslint": "^8.26.0",
48
47
  "rimraf": "^3.0.2",
49
- "typescript": "^4.8.4"
48
+ "typescript": "^4.8.4",
49
+ "vite": "^4.0.1",
50
+ "vite-plugin-dts": "^1.7.1"
50
51
  }
51
52
  }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- import type { Elysia, SCHEMA } from 'elysia'
1
+ import type { Elysia } from 'elysia'
2
2
  import type { HTTPMethod } from 'elysia'
3
3
 
4
- import { CreateEden, EdenCall, UnionToIntersection } from './types'
4
+ import { CreateEden, Eden, EdenCall, UnionToIntersection } from './types'
5
5
 
6
6
  const camelToDash = (str: string) =>
7
7
  str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
@@ -12,8 +12,8 @@ const composePath = (
12
12
  query: EdenCall['$query'] | undefined
13
13
  ) => {
14
14
  if (!domain.endsWith('/')) domain += '/'
15
-
16
15
  path = camelToDash(path.replace(/index/g, ''))
16
+
17
17
  if (!query || !Object.keys(query).length) return `${domain}${path}`
18
18
 
19
19
  let q = ''
@@ -52,7 +52,9 @@ const createProxy = (
52
52
  ? JSON.stringify(body)
53
53
  : undefined,
54
54
  ...$fetch
55
- }).then((res) => {
55
+ }).then(async (res) => {
56
+ if (res.status >= 300) throw new Error(await res.text())
57
+
56
58
  if (res.headers.get('content-type') === 'application/json')
57
59
  return res.json()
58
60
 
@@ -61,14 +63,8 @@ const createProxy = (
61
63
  }
62
64
  }) as unknown as Record<string, unknown>
63
65
 
64
- export function eden<App extends Elysia<any>>(
65
- domain: string
66
- ): App['store'] extends {
67
- [key in typeof SCHEMA]: any
68
- }
69
- ? UnionToIntersection<CreateEden<App['store'][typeof SCHEMA]>>
70
- : never {
71
- return new Proxy(
66
+ export const eden = <App extends Elysia<any>>(domain: string): Eden<App> =>
67
+ new Proxy(
72
68
  {},
73
69
  {
74
70
  get(target, key, value) {
@@ -76,4 +72,3 @@ export function eden<App extends Elysia<any>>(
76
72
  }
77
73
  }
78
74
  ) as any
79
- }
package/src/types.ts CHANGED
@@ -1,4 +1,11 @@
1
- import type { TypedRoute } from 'elysia'
1
+ import type { Elysia, SCHEMA, TypedRoute } from 'elysia'
2
+
3
+ export type Eden<App extends Elysia<any>> = App['store'] extends {
4
+ [key in typeof SCHEMA]: any
5
+ }
6
+ ? UnionToIntersection<CreateEden<App['store'][typeof SCHEMA]>>
7
+ : never
8
+
2
9
 
3
10
  export interface EdenCall {
4
11
  [x: string]: any
@@ -1,5 +0,0 @@
1
- import type { Elysia, SCHEMA } from 'elysia';
2
- import { CreateEden, UnionToIntersection } from './types';
3
- export declare function eden<App extends Elysia<any>>(domain: string): App['store'] extends {
4
- [key in typeof SCHEMA]: any;
5
- } ? UnionToIntersection<CreateEden<App['store'][typeof SCHEMA]>> : never;
package/dist/cjs/index.js DELETED
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.eden = void 0;
4
- const camelToDash = (str) => str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
5
- const composePath = (domain, path, query) => {
6
- if (!domain.endsWith('/'))
7
- domain += '/';
8
- path = camelToDash(path.replace(/index/g, ''));
9
- if (!query || !Object.keys(query).length)
10
- return `${domain}${path}`;
11
- let q = '';
12
- for (const [key, value] of Object.entries(query))
13
- q += `${key}=${value}&`;
14
- return `${domain}${path}?${q.slice(0, -1)}`;
15
- };
16
- const createProxy = (domain, path = '') => new Proxy(() => { }, {
17
- get(target, key, value) {
18
- return createProxy(domain, `${path}/${key.toString()}`);
19
- },
20
- apply(target, _, [{ $query, $fetch, ...body } = {
21
- $fetch: undefined,
22
- $query: undefined
23
- }] = [{}]) {
24
- const i = path.lastIndexOf('/');
25
- return fetch(composePath(domain, path.slice(0, i), $query), {
26
- method: path.slice(i + 1),
27
- headers: {
28
- 'content-type': 'application/json',
29
- ...$fetch === null || $fetch === void 0 ? void 0 : $fetch['headers']
30
- },
31
- body: Object.keys(body).length
32
- ? JSON.stringify(body)
33
- : undefined,
34
- ...$fetch
35
- }).then((res) => {
36
- if (res.headers.get('content-type') === 'application/json')
37
- return res.json();
38
- return res.text();
39
- });
40
- }
41
- });
42
- function eden(domain) {
43
- return new Proxy({}, {
44
- get(target, key, value) {
45
- return createProxy(domain, key);
46
- }
47
- });
48
- }
49
- exports.eden = eden;
@@ -1,26 +0,0 @@
1
- /// <reference types="bun-types" />
2
- import type { TypedRoute } from 'elysia';
3
- export interface EdenCall {
4
- [x: string]: any;
5
- $fetch?: RequestInit;
6
- $query?: Record<string, string | boolean | number>;
7
- }
8
- export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
9
- declare type TypedRouteToParams<Route extends TypedRoute> = (Route['body'] extends NonNullable<Route['body']> ? Route['body'] : {}) & (Route['query'] extends NonNullable<Route['query']> ? {
10
- $query: Route['query'];
11
- } : {});
12
- export declare type CreateEden<Server extends Record<string, Record<string, TypedRoute>>, Path extends string = keyof Server, Full extends string = ''> = Path extends `/${infer Start}` ? CreateEden<Server, Start, Path> : Path extends `${infer A}/${infer B}` ? {
13
- [key in A]: CreateEden<Server, B, Full>;
14
- } : {
15
- [key in Path extends '' ? 'index' : Path extends `:${infer params}` ? string : Path | CamelCase<Path>]: Full extends keyof Server ? {
16
- [key in keyof Server[Full]]: keyof TypedRouteToParams<Server[Full][key]> extends never ? (params?: {
17
- $query?: EdenCall['$query'];
18
- $fetch?: EdenCall['$fetch'];
19
- }) => Promise<Server[Full][key]['response']> : (params: TypedRouteToParams<Server[Full][key]> & {
20
- $query?: EdenCall['$query'];
21
- $fetch?: EdenCall['$fetch'];
22
- }) => Promise<Server[Full][key]['response']>;
23
- } : never;
24
- };
25
- declare type CamelCase<S extends string> = S extends `${infer P1}-${infer P2}${infer P3}` ? `${Lowercase<P1>}${Uppercase<P2>}${CamelCase<P3>}` : Lowercase<S>;
26
- export {};
package/dist/cjs/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};