@porulle/sdk 0.1.0 → 0.5.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.
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Client-side helpers for the uniform API error envelope.
3
+ *
4
+ * The server returns `{ error: { code, message, details?: { issues[] } } }`.
5
+ * `openapi-fetch` surfaces that body as the `error` field of a result, or it
6
+ * may arrive as a thrown value. These helpers normalize either shape so forms
7
+ * can bind field errors without ad-hoc parsing.
8
+ */
9
+ export interface ValidationIssue {
10
+ path: string;
11
+ message: string;
12
+ code?: string;
13
+ }
14
+ export interface ApiErrorBody {
15
+ code: string;
16
+ message: string;
17
+ details?: {
18
+ issues?: ValidationIssue[];
19
+ [key: string]: unknown;
20
+ };
21
+ }
22
+ /** Type guard: is this value a Porulle API error (envelope or bare body)? */
23
+ export declare function isApiError(e: unknown): boolean;
24
+ /**
25
+ * Flatten an API error into field-level and form-level messages for binding
26
+ * to a form. `fieldErrors` is keyed by the issue's dotted path; `formError`
27
+ * is the top-level message.
28
+ *
29
+ * ```ts
30
+ * const { data, error } = await client.POST("/api/things", { body });
31
+ * if (error) {
32
+ * const { fieldErrors, formError } = mapApiErrorToFields(error);
33
+ * }
34
+ * ```
35
+ */
36
+ export declare function mapApiErrorToFields(e: unknown): {
37
+ fieldErrors: Record<string, string>;
38
+ formError: string;
39
+ };
40
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,eAAe,EAAE,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;CAClE;AA+BD,6EAA6E;AAC7E,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAE9C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,GAAG;IAC/C,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,SAAS,EAAE,MAAM,CAAC;CACnB,CASA"}
package/dist/errors.js ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Client-side helpers for the uniform API error envelope.
3
+ *
4
+ * The server returns `{ error: { code, message, details?: { issues[] } } }`.
5
+ * `openapi-fetch` surfaces that body as the `error` field of a result, or it
6
+ * may arrive as a thrown value. These helpers normalize either shape so forms
7
+ * can bind field errors without ad-hoc parsing.
8
+ */
9
+ /** Pull the inner error body out of an envelope, a bare body, or a thrown wrapper. */
10
+ function extractErrorBody(e) {
11
+ if (!e || typeof e !== "object")
12
+ return undefined;
13
+ const obj = e;
14
+ // Full envelope: { error: { code, message, details } }
15
+ if (obj.error && typeof obj.error === "object") {
16
+ const inner = obj.error;
17
+ if (typeof inner.code === "string" && typeof inner.message === "string") {
18
+ return inner;
19
+ }
20
+ }
21
+ // Bare body: { code, message, details }
22
+ if (typeof obj.code === "string" && typeof obj.message === "string") {
23
+ return obj;
24
+ }
25
+ // Thrown wrapper exposing the body on common fields.
26
+ for (const key of ["body", "data", "response"]) {
27
+ if (obj[key]) {
28
+ const nested = extractErrorBody(obj[key]);
29
+ if (nested)
30
+ return nested;
31
+ }
32
+ }
33
+ return undefined;
34
+ }
35
+ /** Type guard: is this value a Porulle API error (envelope or bare body)? */
36
+ export function isApiError(e) {
37
+ return extractErrorBody(e) !== undefined;
38
+ }
39
+ /**
40
+ * Flatten an API error into field-level and form-level messages for binding
41
+ * to a form. `fieldErrors` is keyed by the issue's dotted path; `formError`
42
+ * is the top-level message.
43
+ *
44
+ * ```ts
45
+ * const { data, error } = await client.POST("/api/things", { body });
46
+ * if (error) {
47
+ * const { fieldErrors, formError } = mapApiErrorToFields(error);
48
+ * }
49
+ * ```
50
+ */
51
+ export function mapApiErrorToFields(e) {
52
+ const body = extractErrorBody(e);
53
+ const fieldErrors = {};
54
+ for (const issue of body?.details?.issues ?? []) {
55
+ if (issue.path && fieldErrors[issue.path] === undefined) {
56
+ fieldErrors[issue.path] = issue.message;
57
+ }
58
+ }
59
+ return { fieldErrors, formError: body?.message ?? "" };
60
+ }
61
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAcH,sFAAsF;AACtF,SAAS,gBAAgB,CAAC,CAAU;IAClC,IAAI,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAClD,MAAM,GAAG,GAAG,CAA4B,CAAC;IAEzC,uDAAuD;IACvD,IAAI,GAAG,CAAC,KAAK,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC/C,MAAM,KAAK,GAAG,GAAG,CAAC,KAAgC,CAAC;QACnD,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;YACxE,OAAO,KAAgC,CAAC;QAC1C,CAAC;IACH,CAAC;IAED,wCAAwC;IACxC,IAAI,OAAO,GAAG,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QACpE,OAAO,GAA8B,CAAC;IACxC,CAAC;IAED,qDAAqD;IACrD,KAAK,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,CAAC;QAC/C,IAAI,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACb,MAAM,MAAM,GAAG,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;YAC1C,IAAI,MAAM;gBAAE,OAAO,MAAM,CAAC;QAC5B,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,UAAU,CAAC,CAAU;IACnC,OAAO,gBAAgB,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC;AAC3C,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,mBAAmB,CAAC,CAAU;IAI5C,MAAM,IAAI,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;IACjC,MAAM,WAAW,GAA2B,EAAE,CAAC;IAC/C,KAAK,MAAM,KAAK,IAAI,IAAI,EAAE,OAAO,EAAE,MAAM,IAAI,EAAE,EAAE,CAAC;QAChD,IAAI,KAAK,CAAC,IAAI,IAAI,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;YACxD,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC;QAC1C,CAAC;IACH,CAAC;IACD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,IAAI,EAAE,EAAE,CAAC;AACzD,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { createSDK, createClient, type SDKOptions } from "./client.js";
2
2
  export { authMiddleware, type AuthCredential, type ApiKeyAuth, type BearerAuth } from "./middleware.js";
3
+ export { isApiError, mapApiErrorToFields, type ApiErrorBody, type ValidationIssue, } from "./errors.js";
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,KAAK,cAAc,EAAE,KAAK,UAAU,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,KAAK,cAAc,EAAE,KAAK,UAAU,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACxG,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,KAAK,YAAY,EACjB,KAAK,eAAe,GACrB,MAAM,aAAa,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { createSDK, createClient } from "./client.js";
2
2
  export { authMiddleware } from "./middleware.js";
3
+ export { isApiError, mapApiErrorToFields, } from "./errors.js";
3
4
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAmB,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,cAAc,EAAyD,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAmB,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,cAAc,EAAyD,MAAM,iBAAiB,CAAC;AACxG,OAAO,EACL,UAAU,EACV,mBAAmB,GAGpB,MAAM,aAAa,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@porulle/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -29,8 +29,8 @@
29
29
  "openapi-typescript-helpers": "^0.1.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@repo/eslint-config": "*",
33
- "@repo/typescript-config": "*",
32
+ "@porulle/eslint-config": "*",
33
+ "@porulle/typescript-config": "*",
34
34
  "@types/node": "^24.5.2",
35
35
  "eslint": "^9.39.1",
36
36
  "openapi-react-query": "^0.5.4",
package/src/errors.ts ADDED
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Client-side helpers for the uniform API error envelope.
3
+ *
4
+ * The server returns `{ error: { code, message, details?: { issues[] } } }`.
5
+ * `openapi-fetch` surfaces that body as the `error` field of a result, or it
6
+ * may arrive as a thrown value. These helpers normalize either shape so forms
7
+ * can bind field errors without ad-hoc parsing.
8
+ */
9
+
10
+ export interface ValidationIssue {
11
+ path: string;
12
+ message: string;
13
+ code?: string;
14
+ }
15
+
16
+ export interface ApiErrorBody {
17
+ code: string;
18
+ message: string;
19
+ details?: { issues?: ValidationIssue[]; [key: string]: unknown };
20
+ }
21
+
22
+ /** Pull the inner error body out of an envelope, a bare body, or a thrown wrapper. */
23
+ function extractErrorBody(e: unknown): ApiErrorBody | undefined {
24
+ if (!e || typeof e !== "object") return undefined;
25
+ const obj = e as Record<string, unknown>;
26
+
27
+ // Full envelope: { error: { code, message, details } }
28
+ if (obj.error && typeof obj.error === "object") {
29
+ const inner = obj.error as Record<string, unknown>;
30
+ if (typeof inner.code === "string" && typeof inner.message === "string") {
31
+ return inner as unknown as ApiErrorBody;
32
+ }
33
+ }
34
+
35
+ // Bare body: { code, message, details }
36
+ if (typeof obj.code === "string" && typeof obj.message === "string") {
37
+ return obj as unknown as ApiErrorBody;
38
+ }
39
+
40
+ // Thrown wrapper exposing the body on common fields.
41
+ for (const key of ["body", "data", "response"]) {
42
+ if (obj[key]) {
43
+ const nested = extractErrorBody(obj[key]);
44
+ if (nested) return nested;
45
+ }
46
+ }
47
+
48
+ return undefined;
49
+ }
50
+
51
+ /** Type guard: is this value a Porulle API error (envelope or bare body)? */
52
+ export function isApiError(e: unknown): boolean {
53
+ return extractErrorBody(e) !== undefined;
54
+ }
55
+
56
+ /**
57
+ * Flatten an API error into field-level and form-level messages for binding
58
+ * to a form. `fieldErrors` is keyed by the issue's dotted path; `formError`
59
+ * is the top-level message.
60
+ *
61
+ * ```ts
62
+ * const { data, error } = await client.POST("/api/things", { body });
63
+ * if (error) {
64
+ * const { fieldErrors, formError } = mapApiErrorToFields(error);
65
+ * }
66
+ * ```
67
+ */
68
+ export function mapApiErrorToFields(e: unknown): {
69
+ fieldErrors: Record<string, string>;
70
+ formError: string;
71
+ } {
72
+ const body = extractErrorBody(e);
73
+ const fieldErrors: Record<string, string> = {};
74
+ for (const issue of body?.details?.issues ?? []) {
75
+ if (issue.path && fieldErrors[issue.path] === undefined) {
76
+ fieldErrors[issue.path] = issue.message;
77
+ }
78
+ }
79
+ return { fieldErrors, formError: body?.message ?? "" };
80
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,8 @@
1
1
  export { createSDK, createClient, type SDKOptions } from "./client.js";
2
2
  export { authMiddleware, type AuthCredential, type ApiKeyAuth, type BearerAuth } from "./middleware.js";
3
+ export {
4
+ isApiError,
5
+ mapApiErrorToFields,
6
+ type ApiErrorBody,
7
+ type ValidationIssue,
8
+ } from "./errors.js";