@simplix-react/contract 0.0.2 → 0.1.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.
package/dist/index.d.ts CHANGED
@@ -416,6 +416,10 @@ interface ApiContract<TEntities extends Record<string, EntityDefinition<any, any
416
416
  [K in keyof TEntities]: QueryKeyFactory;
417
417
  };
418
418
  }
419
+ /** Shorthand for an entity definition with any Zod schema types. */
420
+ type AnyEntityDef = EntityDefinition<z.ZodTypeAny, z.ZodTypeAny, z.ZodTypeAny>;
421
+ /** Shorthand for an operation definition with any Zod schema types. */
422
+ type AnyOperationDef = OperationDefinition<z.ZodTypeAny, z.ZodTypeAny>;
419
423
 
420
424
  /**
421
425
  * Creates a fully-typed API contract from an {@link ApiContractConfig}.
@@ -644,7 +648,8 @@ declare function camelToKebab(str: string): string;
644
648
  /**
645
649
  * Converts a camelCase string to snake_case.
646
650
  *
647
- * Used internally for transforming entity names into database-friendly column names.
651
+ * Also handles hyphenated and space-separated inputs by replacing them with
652
+ * underscores before lowercasing.
648
653
  *
649
654
  * @param str - The camelCase string to convert.
650
655
  * @returns The snake_case equivalent.
@@ -655,6 +660,7 @@ declare function camelToKebab(str: string): string;
655
660
  *
656
661
  * camelToSnake("doorReader"); // "door_reader"
657
662
  * camelToSnake("myEntity"); // "my_entity"
663
+ * camelToSnake("some-field"); // "some_field"
658
664
  * ```
659
665
  */
660
666
  declare function camelToSnake(str: string): string;
@@ -688,4 +694,4 @@ declare function camelToSnake(str: string): string;
688
694
  */
689
695
  declare const simpleQueryBuilder: QueryBuilder;
690
696
 
691
- export { type ApiContract, type ApiContractConfig, ApiError, type EntityClient, type EntityDefinition, type EntityParent, type EntityQuery, type FetchFn, type HttpMethod, type ListParams, type OperationDefinition, type PageInfo, type PaginationParam, type QueryBuilder, type QueryKeyFactory, type SortParam, buildPath, camelToKebab, camelToSnake, defaultFetch, defineApi, deriveClient, deriveQueryKeys, simpleQueryBuilder };
697
+ export { type AnyEntityDef, type AnyOperationDef, type ApiContract, type ApiContractConfig, ApiError, type EntityClient, type EntityDefinition, type EntityParent, type EntityQuery, type FetchFn, type HttpMethod, type ListParams, type OperationDefinition, type PageInfo, type PaginationParam, type QueryBuilder, type QueryKeyFactory, type SortParam, buildPath, camelToKebab, camelToSnake, defaultFetch, defineApi, deriveClient, deriveQueryKeys, simpleQueryBuilder };
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  function buildPath(template, params = {}) {
3
3
  let result = template;
4
4
  for (const [key, value] of Object.entries(params)) {
5
- result = result.replace(`:${key}`, encodeURIComponent(value));
5
+ result = result.replaceAll(`:${key}`, encodeURIComponent(value));
6
6
  }
7
7
  return result;
8
8
  }
@@ -17,12 +17,15 @@ var ApiError = class extends Error {
17
17
  }
18
18
  };
19
19
  async function defaultFetch(path, options) {
20
+ const method = options?.method?.toUpperCase();
21
+ const hasBody = method === "POST" || method === "PUT" || method === "PATCH";
22
+ const headers = {
23
+ ...hasBody ? { "Content-Type": "application/json" } : {},
24
+ ...options?.headers
25
+ };
20
26
  const res = await fetch(path, {
21
27
  ...options,
22
- headers: {
23
- "Content-Type": "application/json",
24
- ...options?.headers
25
- }
28
+ headers
26
29
  });
27
30
  if (!res.ok) {
28
31
  throw new ApiError(res.status, await res.text());
@@ -197,7 +200,7 @@ function camelToKebab(str) {
197
200
  return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
198
201
  }
199
202
  function camelToSnake(str) {
200
- return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
203
+ return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[-\s]+/g, "_").toLowerCase();
201
204
  }
202
205
 
203
206
  // src/helpers/query-builders.ts
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@simplix-react/contract",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "description": "Define type-safe API contracts with Zod schemas",
5
5
  "type": "module",
6
+ "sideEffects": false,
6
7
  "exports": {
7
8
  ".": {
8
9
  "types": "./dist/index.d.ts",
@@ -15,24 +16,24 @@
15
16
  "publishConfig": {
16
17
  "access": "public"
17
18
  },
18
- "scripts": {
19
- "build": "tsup",
20
- "dev": "tsup --watch",
21
- "typecheck": "tsc --noEmit",
22
- "lint": "eslint src",
23
- "test": "vitest run --passWithNoTests",
24
- "clean": "rm -rf dist .turbo"
25
- },
26
19
  "peerDependencies": {
27
20
  "zod": ">=4.0.0"
28
21
  },
29
22
  "devDependencies": {
30
- "@simplix-react/config-eslint": "workspace:*",
31
- "@simplix-react/config-typescript": "workspace:*",
32
23
  "eslint": "^9.39.2",
33
24
  "tsup": "^8.5.1",
34
25
  "typescript": "^5.9.3",
35
26
  "vitest": "^3.0.0",
36
- "zod": "^4.0.0"
27
+ "zod": "^4.0.0",
28
+ "@simplix-react/config-typescript": "0.0.1",
29
+ "@simplix-react/config-eslint": "0.0.1"
30
+ },
31
+ "scripts": {
32
+ "build": "tsup",
33
+ "dev": "tsup --watch",
34
+ "typecheck": "tsc --noEmit",
35
+ "lint": "eslint src",
36
+ "test": "vitest run --passWithNoTests",
37
+ "clean": "rm -rf dist .turbo"
37
38
  }
38
- }
39
+ }