@lowerdeck/error 1.0.7 → 1.0.9

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/package.json CHANGED
@@ -1,9 +1,15 @@
1
1
  {
2
2
  "name": "@lowerdeck/error",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
+ "files": [
8
+ "src/**",
9
+ "dist/**",
10
+ "README.md",
11
+ "package.json"
12
+ ],
7
13
  "author": "Tobias Herber",
8
14
  "license": "Apache 2",
9
15
  "type": "module",
@@ -21,15 +27,15 @@
21
27
  "scripts": {
22
28
  "test": "vitest run --passWithNoTests",
23
29
  "lint": "prettier src/**/*.ts --check",
24
- "build": "microbundle"
30
+ "build": "rm -rf ./dist && microbundle"
25
31
  },
26
32
  "dependencies": {
27
- "@lowerdeck/case": "^1.0.8",
28
- "@lowerdeck/validation": "^1.0.3"
33
+ "@lowerdeck/case": "^1.0.9",
34
+ "@lowerdeck/validation": "^1.0.4"
29
35
  },
30
36
  "devDependencies": {
31
37
  "microbundle": "^0.15.1",
32
- "@lowerdeck/tsconfig": "^1.0.0",
38
+ "@lowerdeck/tsconfig": "^1.0.1",
33
39
  "typescript": "5.8.2",
34
40
  "vitest": "^3.1.2"
35
41
  }
@@ -25,7 +25,9 @@ export let badRequestError = createError({
25
25
  message: 'The request is invalid.'
26
26
  });
27
27
 
28
- export function notFoundError(p1: { entity: string; id?: string } & Partial<ErrorData<'not_found', 404>>): ReturnType<typeof createError>;
28
+ export function notFoundError(
29
+ p1: { entity: string; id?: string } & Partial<ErrorData<'not_found', 404>>
30
+ ): ReturnType<typeof createError>;
29
31
  export function notFoundError(p1: string, p2?: string | null): ReturnType<typeof createError>;
30
32
  export function notFoundError(p1: any, p2?: any): ReturnType<typeof createError> {
31
33
  let entity = typeof p1 == 'string' ? p1 : p1.entity;
package/src/error.test.ts CHANGED
@@ -20,7 +20,7 @@ describe('createError', () => {
20
20
  };
21
21
  let errorRecord = createError(data);
22
22
  let response = errorRecord.toResponse();
23
- expect(response).toEqual({ ...data, __typename: 'error', ok: false });
23
+ expect(response).toEqual({ ...data, object: 'error', ok: false });
24
24
  });
25
25
 
26
26
  test('should create an error record with a response function that can be extended', () => {
@@ -33,7 +33,7 @@ describe('createError', () => {
33
33
  let response = errorRecord({ hint: 'This is a hint' }).toResponse();
34
34
  expect(response).toEqual({
35
35
  ...data,
36
- __typename: 'error',
36
+ object: 'error',
37
37
  ok: false,
38
38
  hint: 'This is a hint'
39
39
  });
@@ -69,6 +69,6 @@ describe('ServiceError', () => {
69
69
  });
70
70
  let apiError = new ServiceError(errorRecord);
71
71
  let response = apiError.toResponse();
72
- expect(response).toEqual({ ...errorRecord.data, __typename: 'error', ok: false });
72
+ expect(response).toEqual({ ...errorRecord.data, object: 'error', ok: false });
73
73
  });
74
74
  });
package/src/error.ts CHANGED
@@ -8,31 +8,39 @@ export interface ErrorData<Code extends string, Status extends number> {
8
8
  [key: string]: any;
9
9
  }
10
10
 
11
+ export type ErrorRecordExtension<Code extends string, Status extends number> = <
12
+ NewCode extends string = Code
13
+ >(
14
+ extension?: Partial<ErrorData<NewCode, Status>>
15
+ ) => ErrorRecord<NewCode, Status>;
16
+
11
17
  export type ErrorRecord<Code extends string, Status extends number> = {
12
- __typename: 'error';
18
+ object: 'error';
13
19
  data: ErrorData<Code, Status>;
14
- toResponse: () => ErrorData<Code, Status> & { __typename: 'error'; ok: false };
15
- } & ((extension?: Partial<ErrorData<Code, Status>>) => ErrorRecord<Code, Status>);
20
+ toResponse: () => ErrorData<Code, Status> & { object: 'error'; ok: false };
21
+ } & ErrorRecordExtension<Code, Status>;
16
22
 
17
23
  export let createError = <Code extends string, Status extends number>(
18
24
  data: ErrorData<Code, Status>
19
25
  ): ErrorRecord<Code, Status> => {
20
26
  return Object.assign(
21
- (extension: Partial<ErrorData<Code, Status>> = {}): ErrorRecord<Code, Status> =>
27
+ <NewCode extends string = Code>(
28
+ extension: Partial<ErrorData<NewCode, Status>> = {}
29
+ ): ErrorRecord<NewCode, Status> =>
22
30
  createError({
23
31
  ...data,
24
32
  ...extension
25
- }),
33
+ }) as any,
26
34
  {
27
- __typename: 'error' as const,
35
+ object: 'error' as const,
28
36
  data,
29
- toResponse: () => ({ __typename: 'error' as const, ok: false as const, ...data })
37
+ toResponse: () => ({ object: 'error' as const, ok: false as const, ...data })
30
38
  }
31
- );
39
+ ) as any;
32
40
  };
33
41
 
34
42
  export class ServiceError<InnerError extends ErrorRecord<any, any>> extends Error {
35
- __typename = 'ServiceError' as const;
43
+ object = 'ServiceError' as const;
36
44
 
37
45
  private _parent: Error | null = null;
38
46
 
@@ -60,12 +68,12 @@ export class ServiceError<InnerError extends ErrorRecord<any, any>> extends Erro
60
68
  static fromResponse(raw: ErrorData<any, any>) {
61
69
  let data = raw;
62
70
  delete data.ok;
63
- delete data.__typename;
71
+ delete data.object;
64
72
 
65
73
  return new ServiceError(createError(data));
66
74
  }
67
75
  }
68
76
 
69
77
  export let isServiceError = (e: any): e is ServiceError<any> => {
70
- return e?.__typename === 'ServiceError' || e?.__typename === 'ErrorRecord';
78
+ return e?.object === 'ServiceError' || e?.object === 'ErrorRecord';
71
79
  };
@@ -1,12 +0,0 @@
1
-
2
- $ microbundle
3
- No name was provided for external module '@lowerdeck/case' in output.globals – guessing '_case'
4
- Build "@lowerdeck/error" to dist:
5
- 2.02 kB: index.cjs.gz
6
- 1.73 kB: index.cjs.br
7
- 1332 B: index.module.js.gz
8
- 1080 B: index.module.js.br
9
- 2.03 kB: index.module.js.gz
10
- 1.73 kB: index.module.js.br
11
- 2.1 kB: index.umd.js.gz
12
- 1.79 kB: index.umd.js.br
@@ -1,21 +0,0 @@
1
-
2
- $ vitest run --passWithNoTests
3
- [?25l
4
-  RUN  v3.2.4 /Users/tobias/code/metorial/metorial-enterprise/oss/src/packages/shared/error
5
-
6
- [?2026h
7
-  ❯ src/defaultErrors.test.ts [queued]
8
-
9
-  Test Files 0 passed (2)
10
-  Tests 0 passed (0)
11
-  Start at 10:23:43
12
-  Duration 101ms
13
- [?2026l ✓ src/error.test.ts (6 tests) 2ms
14
- ✓ src/defaultErrors.test.ts (1 test) 1ms
15
-
16
-  Test Files  2 passed (2)
17
-  Tests  7 passed (7)
18
-  Start at  10:23:43
19
-  Duration  242ms (transform 57ms, setup 0ms, collect 75ms, tests 4ms, environment 0ms, prepare 83ms)
20
-
21
- [?25h
package/CHANGELOG.md DELETED
@@ -1,60 +0,0 @@
1
- # @lowerdeck/error
2
-
3
- ## 1.0.7
4
-
5
- ### Patch Changes
6
-
7
- - Fix default entry point
8
- - Updated dependencies
9
- - @lowerdeck/validation@1.0.3
10
- - @lowerdeck/case@1.0.8
11
-
12
- ## 1.0.6
13
-
14
- ### Patch Changes
15
-
16
- - update versions
17
- - Updated dependencies
18
- - @lowerdeck/validation@1.0.2
19
- - @lowerdeck/case@1.0.7
20
-
21
- ## 1.0.5
22
-
23
- ### Patch Changes
24
-
25
- - sync
26
- - Updated dependencies
27
- - @lowerdeck/case@1.0.6
28
-
29
- ## 1.0.4
30
-
31
- ### Patch Changes
32
-
33
- - Fix dist
34
- - Updated dependencies
35
- - @lowerdeck/case@1.0.5
36
-
37
- ## 1.0.3
38
-
39
- ### Patch Changes
40
-
41
- - Fix versions
42
- - Updated dependencies
43
- - @lowerdeck/case@1.0.4
44
-
45
- ## 1.0.2
46
-
47
- ### Patch Changes
48
-
49
- - Update case
50
- - Updated dependencies
51
- - @lowerdeck/case@1.0.3
52
-
53
- ## 1.0.1
54
-
55
- ### Patch Changes
56
-
57
- - Fix exports
58
- - Updated dependencies
59
- - @lowerdeck/validation@1.0.1
60
- - @lowerdeck/case@1.0.1
@@ -1,2 +0,0 @@
1
- import{Cases as e}from"@lowerdeck/case";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var s=arguments[t];for(var r in s)({}).hasOwnProperty.call(s,r)&&(e[r]=s[r])}return e},t.apply(null,arguments)}let s=e=>Object.assign((r={})=>s(t({},e,r)),{__typename:"error",data:e,toResponse:()=>t({__typename:"error",ok:!1},e)});class r extends Error{get parent(){return this._parent}constructor(e){super(e.data.message),this.error=void 0,this.__typename="ServiceError",this._parent=null,this.error=e}setParent(e){return this._parent=e,this}get data(){return this.error.data}toResponse(){return this.error.toResponse()}static fromResponse(e){let t=e;return delete t.ok,delete t.__typename,new r(s(t))}}let o=e=>"ServiceError"===(null==e?void 0:e.__typename)||"ErrorRecord"===(null==e?void 0:e.__typename),a=e=>s(t({status:400,code:"invalid_data",message:"The provided data is invalid.",hint:"Make sure the data you are sending follows the specification outlined in the documentation."},e)),n=s({status:500,code:"internal_server_error",message:"An internal server error occurred."}),i=s({status:400,code:"bad_request",message:"The request is invalid."});function d(r,o){let a="string"==typeof r?r:r.entity,n="string"==typeof r?o:r.id;return s(t({status:404,code:"not_found",message:`The requested ${e.toKebabCase(a)} could not be found.`,hint:"Make sure the resource you are trying to access exists, that you have access to it and that it has not been deleted.",entity:a,id:null!=n?n:void 0},"string"==typeof r?{}:r))}let u=s({status:401,code:"unauthorized",message:"You are not authorized to access this resource.",hint:"Make sure you are logged in and have the correct permissions to access this resource."}),c=s({status:403,code:"forbidden",message:"You do not have permission to access this resource."}),l=s({status:400,code:"invalid_version",message:"The endpoint does not support the requested version."}),h=s({status:409,code:"conflict",message:"A similar resource already exists."}),m=s({status:410,code:"gone",message:"The requested resource is no longer available."}),p=s({status:402,code:"payment_required",message:"Payment is required to access this resource."}),g=s({status:412,code:"precondition_failed",message:"The precondition of the request failed."}),_=s({status:406,code:"not_acceptable",message:"The requested resource is not acceptable."}),y=s({status:501,code:"not_implemented",message:"The requested resource is not implemented."}),v=s({status:429,code:"too_many_requests",message:"You have made too many requests in a short period of time.",hint:"Please wait a moment and try again."}),f=s({status:504,code:"timeout",message:"The request timed out."}),q=s({status:405,code:"method_not_allowed",message:"The requested method is not allowed for this resource."});export{r as ServiceError,i as badRequestError,h as conflictError,s as createError,c as forbiddenError,m as goneError,n as internalServerError,l as invalidVersion,o as isServiceError,q as methodNotAllowedError,_ as notAcceptableError,d as notFoundError,y as notImplementedError,p as paymentRequiredError,g as preconditionFailedError,f as timeoutError,v as tooManyRequestsError,u as unauthorizedError,a as validationError};
2
- //# sourceMappingURL=index.modern.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.modern.js","sources":["../src/error.ts","../src/defaultErrors.ts"],"sourcesContent":["export interface ErrorData<Code extends string, Status extends number> {\n status: Status;\n code: Code;\n message: string;\n hint?: string;\n description?: string;\n reason?: string;\n [key: string]: any;\n}\n\nexport type ErrorRecord<Code extends string, Status extends number> = {\n __typename: 'error';\n data: ErrorData<Code, Status>;\n toResponse: () => ErrorData<Code, Status> & { __typename: 'error'; ok: false };\n} & ((extension?: Partial<ErrorData<Code, Status>>) => ErrorRecord<Code, Status>);\n\nexport let createError = <Code extends string, Status extends number>(\n data: ErrorData<Code, Status>\n): ErrorRecord<Code, Status> => {\n return Object.assign(\n (extension: Partial<ErrorData<Code, Status>> = {}): ErrorRecord<Code, Status> =>\n createError({\n ...data,\n ...extension\n }),\n {\n __typename: 'error' as const,\n data,\n toResponse: () => ({ __typename: 'error' as const, ok: false as const, ...data })\n }\n );\n};\n\nexport class ServiceError<InnerError extends ErrorRecord<any, any>> extends Error {\n __typename = 'ServiceError' as const;\n\n private _parent: Error | null = null;\n\n public get parent() {\n return this._parent;\n }\n\n constructor(private readonly error: InnerError) {\n super(error.data.message);\n }\n\n setParent(parent: Error) {\n this._parent = parent;\n return this;\n }\n\n get data() {\n return this.error.data;\n }\n\n toResponse() {\n return this.error.toResponse();\n }\n\n static fromResponse(raw: ErrorData<any, any>) {\n let data = raw;\n delete data.ok;\n delete data.__typename;\n\n return new ServiceError(createError(data));\n }\n}\n\nexport let isServiceError = (e: any): e is ServiceError<any> => {\n return e?.__typename === 'ServiceError' || e?.__typename === 'ErrorRecord';\n};\n","import { Cases } from '@lowerdeck/case';\nimport { ValidationError } from '@lowerdeck/validation';\nimport { ErrorData, createError } from './error';\n\nexport let validationError = (\n d: { errors: ValidationError[]; entity: string } & Partial<ErrorData<'invalid_data', 400>>\n) =>\n createError({\n status: 400,\n code: 'invalid_data',\n message: 'The provided data is invalid.',\n hint: 'Make sure the data you are sending follows the specification outlined in the documentation.',\n ...d\n });\n\nexport let internalServerError = createError({\n status: 500,\n code: 'internal_server_error',\n message: 'An internal server error occurred.'\n});\n\nexport let badRequestError = createError({\n status: 400,\n code: 'bad_request',\n message: 'The request is invalid.'\n});\n\nexport function notFoundError(p1: { entity: string; id?: string } & Partial<ErrorData<'not_found', 404>>): ReturnType<typeof createError>;\nexport function notFoundError(p1: string, p2?: string | null): ReturnType<typeof createError>;\nexport function notFoundError(p1: any, p2?: any): ReturnType<typeof createError> {\n let entity = typeof p1 == 'string' ? p1 : p1.entity;\n let id = typeof p1 == 'string' ? p2 : p1.id;\n\n return createError({\n status: 404,\n code: 'not_found',\n message: `The requested ${Cases.toKebabCase(entity)} could not be found.`,\n hint: 'Make sure the resource you are trying to access exists, that you have access to it and that it has not been deleted.',\n entity,\n id: id ?? undefined,\n\n ...(typeof p1 == 'string' ? {} : p1)\n });\n}\n\nexport let unauthorizedError = createError({\n status: 401,\n code: 'unauthorized',\n message: 'You are not authorized to access this resource.',\n hint: 'Make sure you are logged in and have the correct permissions to access this resource.'\n});\n\nexport let forbiddenError = createError({\n status: 403,\n code: 'forbidden',\n message: 'You do not have permission to access this resource.'\n});\n\nexport let invalidVersion = createError({\n status: 400,\n code: 'invalid_version',\n message: 'The endpoint does not support the requested version.'\n});\n\nexport let conflictError = createError({\n status: 409,\n code: 'conflict',\n message: 'A similar resource already exists.'\n});\n\nexport let goneError = createError({\n status: 410,\n code: 'gone',\n message: 'The requested resource is no longer available.'\n});\n\nexport let paymentRequiredError = createError({\n status: 402,\n code: 'payment_required',\n message: 'Payment is required to access this resource.'\n});\n\nexport let preconditionFailedError = createError({\n status: 412,\n code: 'precondition_failed',\n message: 'The precondition of the request failed.'\n});\n\nexport let notAcceptableError = createError({\n status: 406,\n code: 'not_acceptable',\n message: 'The requested resource is not acceptable.'\n});\n\nexport let notImplementedError = createError({\n status: 501,\n code: 'not_implemented',\n message: 'The requested resource is not implemented.'\n});\n\nexport let tooManyRequestsError = createError({\n status: 429,\n code: 'too_many_requests',\n message: 'You have made too many requests in a short period of time.',\n hint: 'Please wait a moment and try again.'\n});\n\nexport let timeoutError = createError({\n status: 504,\n code: 'timeout',\n message: 'The request timed out.'\n});\n\nexport let methodNotAllowedError = createError({\n status: 405,\n code: 'method_not_allowed',\n message: 'The requested method is not allowed for this resource.'\n});\n"],"names":["createError","data","Object","assign","extension","_extends","__typename","toResponse","ok","ServiceError","Error","parent","this","_parent","constructor","error","super","message","setParent","fromResponse","raw","isServiceError","e","validationError","d","status","code","hint","internalServerError","badRequestError","notFoundError","p1","p2","entity","id","Cases","toKebabCase","undefined","unauthorizedError","forbiddenError","invalidVersion","conflictError","goneError","paymentRequiredError","preconditionFailedError","notAcceptableError","notImplementedError","tooManyRequestsError","timeoutError","methodNotAllowedError"],"mappings":"gQAgBW,IAAAA,EACTC,GAEOC,OAAOC,OACZ,CAACC,EAA8C,CAAA,IAC7CJ,EAAWK,EACNJ,CAAAA,EAAAA,EACAG,IAEP,CACEE,WAAY,QACZL,OACAM,WAAYA,IAAAF,EAAA,CAASC,WAAY,QAAkBE,IAAI,GAAmBP,WAKnEQ,UAA+DC,MAK1E,UAAWC,GACT,OAAWC,KAACC,OACd,CAEAC,WAAAA,CAA6BC,GAC3BC,MAAMD,EAAMd,KAAKgB,SAASL,KADCG,WAR7BT,EAAAA,KAAAA,WAAa,eAELO,KAAAA,QAAwB,KAMHD,KAAKG,MAALA,CAE7B,CAEAG,SAAAA,CAAUP,GAER,OADAC,KAAKC,QAAUF,EAEjBC,IAAA,CAEA,QAAIX,GACF,OAAOW,KAAKG,MAAMd,IACpB,CAEAM,UAAAA,GACE,OAAOK,KAAKG,MAAMR,YACpB,CAEA,mBAAOY,C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yB,OAAQ,IACRC,KAAM,iBACNT,QAAS,8CAGA6B,EAAsB9C,EAAY,CAC3CyB,OAAQ,IACRC,KAAM,kBACNT,QAAS,+CAGA8B,EAAuB/C,EAAY,CAC5CyB,OAAQ,IACRC,KAAM,oBACNT,QAAS,6DACTU,KAAM,wCAGGqB,EAAehD,EAAY,CACpCyB,OAAQ,IACRC,KAAM,UACNT,QAAS,2BAGAgC,EAAwBjD,EAAY,CAC7CyB,OAAQ,IACRC,KAAM,qBACNT,QAAS"}
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "$schema": "https://json.schemastore.org/tsconfig",
3
- "extends": "@lowerdeck/tsconfig/base.json",
4
- "exclude": ["dist"],
5
- "include": ["src"],
6
- "compilerOptions": {
7
- "outDir": "dist",
8
- "lib": ["es2021"],
9
- "target": "ES2019"
10
- }
11
- }