@elysiajs/eden 0.1.0-rc.1 → 0.1.0-rc.3

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/cjs/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.eden = void 0;
4
+ const camelToDash = (str) => str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
4
5
  const composePath = (domain, path, query) => {
5
6
  if (!domain.endsWith('/'))
6
7
  domain += '/';
7
- path = path.replace(/index/g, '');
8
+ path = camelToDash(path.replace(/index/g, ''));
8
9
  if (!query || !Object.keys(query).length)
9
10
  return `${domain}${path}`;
10
11
  let q = '';
@@ -31,7 +32,9 @@ const createProxy = (domain, path = '') => new Proxy(() => { }, {
31
32
  ? JSON.stringify(body)
32
33
  : undefined,
33
34
  ...$fetch
34
- }).then((res) => {
35
+ }).then(async (res) => {
36
+ if (res.status >= 300)
37
+ throw new Error(await res.text());
35
38
  if (res.headers.get('content-type') === 'application/json')
36
39
  return res.json();
37
40
  return res.text();
@@ -12,7 +12,7 @@ declare type TypedRouteToParams<Route extends TypedRoute> = (Route['body'] exten
12
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
13
  [key in A]: CreateEden<Server, B, Full>;
14
14
  } : {
15
- [key in Path extends '' ? 'index' : Path extends `:${infer params}` ? string : Path]: Full extends keyof Server ? {
15
+ [key in Path extends '' ? 'index' : Path extends `:${infer params}` ? string : Path | CamelCase<Path>]: Full extends keyof Server ? {
16
16
  [key in keyof Server[Full]]: keyof TypedRouteToParams<Server[Full][key]> extends never ? (params?: {
17
17
  $query?: EdenCall['$query'];
18
18
  $fetch?: EdenCall['$fetch'];
@@ -22,4 +22,5 @@ export declare type CreateEden<Server extends Record<string, Record<string, Type
22
22
  }) => Promise<Server[Full][key]['response']>;
23
23
  } : never;
24
24
  };
25
+ declare type CamelCase<S extends string> = S extends `${infer P1}-${infer P2}${infer P3}` ? `${Lowercase<P1>}${Uppercase<P2>}${CamelCase<P3>}` : Lowercase<S>;
25
26
  export {};
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
+ const camelToDash = (str) => str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
1
2
  const composePath = (domain, path, query) => {
2
3
  if (!domain.endsWith('/'))
3
4
  domain += '/';
4
- path = path.replace(/index/g, '');
5
+ path = camelToDash(path.replace(/index/g, ''));
5
6
  if (!query || !Object.keys(query).length)
6
7
  return `${domain}${path}`;
7
8
  let q = '';
@@ -28,7 +29,9 @@ const createProxy = (domain, path = '') => new Proxy(() => { }, {
28
29
  ? JSON.stringify(body)
29
30
  : undefined,
30
31
  ...$fetch
31
- }).then((res) => {
32
+ }).then(async (res) => {
33
+ if (res.status >= 300)
34
+ throw new Error(await res.text());
32
35
  if (res.headers.get('content-type') === 'application/json')
33
36
  return res.json();
34
37
  return res.text();
package/dist/types.d.ts CHANGED
@@ -12,7 +12,7 @@ declare type TypedRouteToParams<Route extends TypedRoute> = (Route['body'] exten
12
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
13
  [key in A]: CreateEden<Server, B, Full>;
14
14
  } : {
15
- [key in Path extends '' ? 'index' : Path extends `:${infer params}` ? string : Path]: Full extends keyof Server ? {
15
+ [key in Path extends '' ? 'index' : Path extends `:${infer params}` ? string : Path | CamelCase<Path>]: Full extends keyof Server ? {
16
16
  [key in keyof Server[Full]]: keyof TypedRouteToParams<Server[Full][key]> extends never ? (params?: {
17
17
  $query?: EdenCall['$query'];
18
18
  $fetch?: EdenCall['$fetch'];
@@ -22,4 +22,5 @@ export declare type CreateEden<Server extends Record<string, Record<string, Type
22
22
  }) => Promise<Server[Full][key]['response']>;
23
23
  } : never;
24
24
  };
25
+ declare type CamelCase<S extends string> = S extends `${infer P1}-${infer P2}${infer P3}` ? `${Lowercase<P1>}${Uppercase<P2>}${CamelCase<P3>}` : Lowercase<S>;
25
26
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elysiajs/eden",
3
- "version": "0.1.0-rc.1",
3
+ "version": "0.1.0-rc.3",
4
4
  "description": "Fully type-safe Elysia client",
5
5
  "author": {
6
6
  "name": "saltyAom",
@@ -36,14 +36,14 @@
36
36
  "release": "npm run build && npm run test && npm publish --access public"
37
37
  },
38
38
  "peerDependencies": {
39
- "elysia": ">= 0.1.0-rc.4"
39
+ "elysia": ">= 0.1.0-rc.5"
40
40
  },
41
41
  "devDependencies": {
42
- "@elysiajs/cors": "^0.1.0-rc.2",
42
+ "@elysiajs/cors": "^0.1.0-rc.3",
43
43
  "@sinclair/typebox": "^0.25.13",
44
44
  "@types/node": "^18.11.7",
45
45
  "bun-types": "^0.2.2",
46
- "elysia": "^0.1.0-rc.4",
46
+ "elysia": "^0.1.0-rc.5",
47
47
  "eslint": "^8.26.0",
48
48
  "rimraf": "^3.0.2",
49
49
  "typescript": "^4.8.4"
package/src/index.ts CHANGED
@@ -3,6 +3,9 @@ import type { HTTPMethod } from 'elysia'
3
3
 
4
4
  import { CreateEden, EdenCall, UnionToIntersection } from './types'
5
5
 
6
+ const camelToDash = (str: string) =>
7
+ str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
8
+
6
9
  const composePath = (
7
10
  domain: string,
8
11
  path: string,
@@ -10,7 +13,7 @@ const composePath = (
10
13
  ) => {
11
14
  if (!domain.endsWith('/')) domain += '/'
12
15
 
13
- path = path.replace(/index/g, '')
16
+ path = camelToDash(path.replace(/index/g, ''))
14
17
  if (!query || !Object.keys(query).length) return `${domain}${path}`
15
18
 
16
19
  let q = ''
@@ -49,7 +52,9 @@ const createProxy = (
49
52
  ? JSON.stringify(body)
50
53
  : undefined,
51
54
  ...$fetch
52
- }).then((res) => {
55
+ }).then(async (res) => {
56
+ if (res.status >= 300) throw new Error(await res.text())
57
+
53
58
  if (res.headers.get('content-type') === 'application/json')
54
59
  return res.json()
55
60
 
package/src/types.ts CHANGED
@@ -36,7 +36,7 @@ export type CreateEden<
36
36
  ? 'index'
37
37
  : Path extends `:${infer params}`
38
38
  ? string
39
- : Path]: Full extends keyof Server
39
+ : Path | CamelCase<Path>]: Full extends keyof Server
40
40
  ? {
41
41
  [key in keyof Server[Full]]: keyof TypedRouteToParams<
42
42
  Server[Full][key]
@@ -54,3 +54,11 @@ export type CreateEden<
54
54
  }
55
55
  : never
56
56
  }
57
+
58
+ // https://stackoverflow.com/questions/59623524/typescript-how-to-map-type-keys-to-camelcase
59
+ type CamelCase<S extends string> =
60
+ S extends `${infer P1}-${infer P2}${infer P3}`
61
+ ? `${Lowercase<P1>}${Uppercase<P2>}${CamelCase<P3>}`
62
+ : Lowercase<S>
63
+
64
+ type A = CamelCase<'sign-in'>