@uploadista/server 0.0.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.
Files changed (91) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/.turbo/turbo-check.log +34 -0
  3. package/LICENSE +21 -0
  4. package/README.md +503 -0
  5. package/dist/auth/cache.d.ts +87 -0
  6. package/dist/auth/cache.d.ts.map +1 -0
  7. package/dist/auth/cache.js +121 -0
  8. package/dist/auth/cache.test.d.ts +2 -0
  9. package/dist/auth/cache.test.d.ts.map +1 -0
  10. package/dist/auth/cache.test.js +209 -0
  11. package/dist/auth/get-auth-credentials.d.ts +73 -0
  12. package/dist/auth/get-auth-credentials.d.ts.map +1 -0
  13. package/dist/auth/get-auth-credentials.js +55 -0
  14. package/dist/auth/index.d.ts +2 -0
  15. package/dist/auth/index.d.ts.map +1 -0
  16. package/dist/auth/index.js +1 -0
  17. package/dist/auth/jwt/index.d.ts +38 -0
  18. package/dist/auth/jwt/index.d.ts.map +1 -0
  19. package/dist/auth/jwt/index.js +36 -0
  20. package/dist/auth/jwt/types.d.ts +77 -0
  21. package/dist/auth/jwt/types.d.ts.map +1 -0
  22. package/dist/auth/jwt/types.js +1 -0
  23. package/dist/auth/jwt/validate.d.ts +58 -0
  24. package/dist/auth/jwt/validate.d.ts.map +1 -0
  25. package/dist/auth/jwt/validate.js +226 -0
  26. package/dist/auth/jwt/validate.test.d.ts +2 -0
  27. package/dist/auth/jwt/validate.test.d.ts.map +1 -0
  28. package/dist/auth/jwt/validate.test.js +492 -0
  29. package/dist/auth/service.d.ts +63 -0
  30. package/dist/auth/service.d.ts.map +1 -0
  31. package/dist/auth/service.js +43 -0
  32. package/dist/auth/service.test.d.ts +2 -0
  33. package/dist/auth/service.test.d.ts.map +1 -0
  34. package/dist/auth/service.test.js +195 -0
  35. package/dist/auth/types.d.ts +38 -0
  36. package/dist/auth/types.d.ts.map +1 -0
  37. package/dist/auth/types.js +1 -0
  38. package/dist/cache.d.ts +87 -0
  39. package/dist/cache.d.ts.map +1 -0
  40. package/dist/cache.js +121 -0
  41. package/dist/cache.test.d.ts +2 -0
  42. package/dist/cache.test.d.ts.map +1 -0
  43. package/dist/cache.test.js +209 -0
  44. package/dist/cloudflare-config.d.ts +72 -0
  45. package/dist/cloudflare-config.d.ts.map +1 -0
  46. package/dist/cloudflare-config.js +67 -0
  47. package/dist/error-types.d.ts +138 -0
  48. package/dist/error-types.d.ts.map +1 -0
  49. package/dist/error-types.js +155 -0
  50. package/dist/hono-adapter.d.ts +48 -0
  51. package/dist/hono-adapter.d.ts.map +1 -0
  52. package/dist/hono-adapter.js +58 -0
  53. package/dist/http-utils.d.ts +148 -0
  54. package/dist/http-utils.d.ts.map +1 -0
  55. package/dist/http-utils.js +233 -0
  56. package/dist/index.d.ts +9 -0
  57. package/dist/index.d.ts.map +1 -0
  58. package/dist/index.js +8 -0
  59. package/dist/layer-utils.d.ts +121 -0
  60. package/dist/layer-utils.d.ts.map +1 -0
  61. package/dist/layer-utils.js +80 -0
  62. package/dist/metrics/service.d.ts +26 -0
  63. package/dist/metrics/service.d.ts.map +1 -0
  64. package/dist/metrics/service.js +20 -0
  65. package/dist/plugins-typing.d.ts +11 -0
  66. package/dist/plugins-typing.d.ts.map +1 -0
  67. package/dist/plugins-typing.js +1 -0
  68. package/dist/service.d.ts +63 -0
  69. package/dist/service.d.ts.map +1 -0
  70. package/dist/service.js +43 -0
  71. package/dist/service.test.d.ts +2 -0
  72. package/dist/service.test.d.ts.map +1 -0
  73. package/dist/service.test.js +195 -0
  74. package/dist/types.d.ts +38 -0
  75. package/dist/types.d.ts.map +1 -0
  76. package/dist/types.js +1 -0
  77. package/package.json +47 -0
  78. package/src/auth/get-auth-credentials.ts +97 -0
  79. package/src/auth/index.ts +1 -0
  80. package/src/cache.test.ts +306 -0
  81. package/src/cache.ts +204 -0
  82. package/src/error-types.ts +172 -0
  83. package/src/http-utils.ts +264 -0
  84. package/src/index.ts +8 -0
  85. package/src/layer-utils.ts +184 -0
  86. package/src/plugins-typing.ts +57 -0
  87. package/src/service.test.ts +275 -0
  88. package/src/service.ts +78 -0
  89. package/src/types.ts +40 -0
  90. package/tsconfig.json +13 -0
  91. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,72 @@
1
+ import type { EventEmitter, KvStore, UploadEvent, UploadFile } from "@uploadista/core/types";
2
+ import type { UploadServerOptions } from "@uploadista/core/upload";
3
+ import type { Database } from "@uploadista/orm";
4
+ export type CloudflareWorkerEnv = {
5
+ DATABASE_URL: string;
6
+ UPLOADISTA_KV: KVNamespace;
7
+ STORAGE_ENCRYPTION_KEY: string;
8
+ STORAGE_ENCRYPTION_IV_LENGTH: string;
9
+ UPLOAD_EVENT_DURABLE_OBJECT: DurableObjectNamespace<any>;
10
+ };
11
+ export type CloudflareUploadServerConfig = {
12
+ env: CloudflareWorkerEnv;
13
+ db: Database;
14
+ kvStore: KvStore<UploadFile>;
15
+ eventEmitter: EventEmitter<UploadEvent>;
16
+ getDataStore: (storageId: string, options?: {
17
+ db?: Database;
18
+ kvStore?: KvStore<UploadFile>;
19
+ storageEncryptionKey?: string;
20
+ storageIvLength?: string;
21
+ }) => Promise<any>;
22
+ };
23
+ /**
24
+ * Create an upload server configuration for Cloudflare Workers
25
+ *
26
+ * This factory function helps configure the upload server with common
27
+ * Cloudflare Workers dependencies and environment variables.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { createCloudflareUploadServerConfig } from "@uploadista/server";
32
+ * import { createHonoUploadAdapter } from "@uploadista/server";
33
+ *
34
+ * const uploadConfig = createCloudflareUploadServerConfig({
35
+ * env: c.env,
36
+ * db,
37
+ * kvStore,
38
+ * eventEmitter,
39
+ * getDataStore: myGetDataStore,
40
+ * });
41
+ *
42
+ * const uploadAdapter = createHonoUploadAdapter(uploadConfig);
43
+ * ```
44
+ */
45
+ export declare const createCloudflareUploadServerConfig: (config: CloudflareUploadServerConfig) => UploadServerOptions;
46
+ /**
47
+ * Simplified configuration builder for common Cloudflare Workers setups
48
+ *
49
+ * This provides a more opinionated configuration that works well for most
50
+ * standard Cloudflare Workers deployments.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import { createSimpleCloudflareConfig } from "@uploadista/server";
55
+ *
56
+ * const uploadAdapter = createSimpleCloudflareConfig({
57
+ * env: c.env,
58
+ * db,
59
+ * getDataStore: myGetDataStore,
60
+ * createKvStore: () => myKvStore,
61
+ * createEventEmitter: () => myEventEmitter,
62
+ * });
63
+ * ```
64
+ */
65
+ export declare const createSimpleCloudflareConfig: (config: {
66
+ env: CloudflareWorkerEnv;
67
+ db: Database;
68
+ getDataStore: CloudflareUploadServerConfig["getDataStore"];
69
+ createKvStore: () => KvStore<UploadFile>;
70
+ createEventEmitter: () => EventEmitter<UploadEvent>;
71
+ }) => UploadServerOptions;
72
+ //# sourceMappingURL=cloudflare-config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare-config.d.ts","sourceRoot":"","sources":["../src/cloudflare-config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,OAAO,EACP,WAAW,EACX,UAAU,EACX,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEhD,MAAM,MAAM,mBAAmB,GAAG;IAChC,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,WAAW,CAAC;IAC3B,sBAAsB,EAAE,MAAM,CAAC;IAC/B,4BAA4B,EAAE,MAAM,CAAC;IACrC,2BAA2B,EAAE,sBAAsB,CAAC,GAAG,CAAC,CAAC;CAC1D,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG;IACzC,GAAG,EAAE,mBAAmB,CAAC;IACzB,EAAE,EAAE,QAAQ,CAAC;IACb,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IAC7B,YAAY,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC;IACxC,YAAY,EAAE,CACZ,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE;QACR,EAAE,CAAC,EAAE,QAAQ,CAAC;QACd,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;QAC9B,oBAAoB,CAAC,EAAE,MAAM,CAAC;QAC9B,eAAe,CAAC,EAAE,MAAM,CAAC;KAC1B,KACE,OAAO,CAAC,GAAG,CAAC,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,kCAAkC,GAC7C,QAAQ,4BAA4B,KACnC,mBAeF,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,4BAA4B,GAAI,QAAQ;IACnD,GAAG,EAAE,mBAAmB,CAAC;IACzB,EAAE,EAAE,QAAQ,CAAC;IACb,YAAY,EAAE,4BAA4B,CAAC,cAAc,CAAC,CAAC;IAC3D,aAAa,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IACzC,kBAAkB,EAAE,MAAM,YAAY,CAAC,WAAW,CAAC,CAAC;CACrD,wBAYA,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Create an upload server configuration for Cloudflare Workers
3
+ *
4
+ * This factory function helps configure the upload server with common
5
+ * Cloudflare Workers dependencies and environment variables.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { createCloudflareUploadServerConfig } from "@uploadista/server";
10
+ * import { createHonoUploadAdapter } from "@uploadista/server";
11
+ *
12
+ * const uploadConfig = createCloudflareUploadServerConfig({
13
+ * env: c.env,
14
+ * db,
15
+ * kvStore,
16
+ * eventEmitter,
17
+ * getDataStore: myGetDataStore,
18
+ * });
19
+ *
20
+ * const uploadAdapter = createHonoUploadAdapter(uploadConfig);
21
+ * ```
22
+ */
23
+ export const createCloudflareUploadServerConfig = (config) => {
24
+ const { env, db, kvStore, eventEmitter, getDataStore } = config;
25
+ return {
26
+ dataStore: (storageId) => {
27
+ return getDataStore(storageId, {
28
+ db,
29
+ kvStore,
30
+ storageEncryptionKey: env.STORAGE_ENCRYPTION_KEY,
31
+ storageIvLength: env.STORAGE_ENCRYPTION_IV_LENGTH,
32
+ });
33
+ },
34
+ kvStore,
35
+ eventEmitter,
36
+ };
37
+ };
38
+ /**
39
+ * Simplified configuration builder for common Cloudflare Workers setups
40
+ *
41
+ * This provides a more opinionated configuration that works well for most
42
+ * standard Cloudflare Workers deployments.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { createSimpleCloudflareConfig } from "@uploadista/server";
47
+ *
48
+ * const uploadAdapter = createSimpleCloudflareConfig({
49
+ * env: c.env,
50
+ * db,
51
+ * getDataStore: myGetDataStore,
52
+ * createKvStore: () => myKvStore,
53
+ * createEventEmitter: () => myEventEmitter,
54
+ * });
55
+ * ```
56
+ */
57
+ export const createSimpleCloudflareConfig = (config) => {
58
+ const { env, db, getDataStore, createKvStore, createEventEmitter } = config;
59
+ const uploadServerConfig = createCloudflareUploadServerConfig({
60
+ env,
61
+ db,
62
+ kvStore: createKvStore(),
63
+ eventEmitter: createEventEmitter(),
64
+ getDataStore,
65
+ });
66
+ return uploadServerConfig;
67
+ };
@@ -0,0 +1,138 @@
1
+ import type { UploadistaError } from "@uploadista/core/errors";
2
+ /**
3
+ * Base adapter error class for HTTP adapters.
4
+ * All adapter-specific errors should extend this class or one of its subclasses.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * throw new AdapterError("Something went wrong", 500, "INTERNAL_ERROR");
9
+ * ```
10
+ */
11
+ export declare class AdapterError extends Error {
12
+ readonly statusCode: number;
13
+ readonly errorCode: string;
14
+ constructor(message: string, statusCode?: number, errorCode?: string);
15
+ }
16
+ /**
17
+ * Validation error - indicates invalid request data or parameters.
18
+ * Returns HTTP 400 Bad Request status.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * if (!isValidUploadId(id)) {
23
+ * throw new ValidationError("Invalid upload ID format");
24
+ * }
25
+ * ```
26
+ */
27
+ export declare class ValidationError extends AdapterError {
28
+ constructor(message: string);
29
+ }
30
+ /**
31
+ * Not found error - indicates a requested resource does not exist.
32
+ * Returns HTTP 404 Not Found status.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * if (!upload) {
37
+ * throw new NotFoundError("Upload");
38
+ * }
39
+ * ```
40
+ */
41
+ export declare class NotFoundError extends AdapterError {
42
+ constructor(resource: string);
43
+ }
44
+ /**
45
+ * Bad request error - indicates a malformed request.
46
+ * Returns HTTP 400 Bad Request status.
47
+ * Similar to ValidationError but for request structure issues.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * try {
52
+ * const data = JSON.parse(body);
53
+ * } catch {
54
+ * throw new BadRequestError("Invalid JSON body");
55
+ * }
56
+ * ```
57
+ */
58
+ export declare class BadRequestError extends AdapterError {
59
+ constructor(message: string);
60
+ }
61
+ /**
62
+ * Creates a standardized error response object for AdapterError.
63
+ * Includes error message, error code, and ISO timestamp.
64
+ *
65
+ * @param error - The AdapterError to format
66
+ * @returns Standardized error response body
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * import { createErrorResponseBody } from "@uploadista/server";
71
+ *
72
+ * try {
73
+ * // ... operation
74
+ * } catch (err) {
75
+ * const errorResponse = createErrorResponseBody(err);
76
+ * res.status(err.statusCode).json(errorResponse);
77
+ * }
78
+ * ```
79
+ */
80
+ export declare const createErrorResponseBody: (error: AdapterError) => {
81
+ error: string;
82
+ code: string;
83
+ timestamp: string;
84
+ };
85
+ /**
86
+ * Creates a standardized error response body from UploadistaError.
87
+ * Formats core library errors for HTTP responses with optional details.
88
+ *
89
+ * @param error - The UploadistaError to format
90
+ * @returns Standardized error response body with error, code, timestamp, and optional details
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * import { createUploadistaErrorResponseBody } from "@uploadista/server";
95
+ *
96
+ * try {
97
+ * const result = yield* uploadServer.handleUpload(input);
98
+ * } catch (err) {
99
+ * if (err instanceof UploadistaError) {
100
+ * const errorResponse = createUploadistaErrorResponseBody(err);
101
+ * res.status(400).json(errorResponse);
102
+ * }
103
+ * }
104
+ * ```
105
+ */
106
+ export declare const createUploadistaErrorResponseBody: (error: UploadistaError) => {
107
+ error: string;
108
+ code: string;
109
+ timestamp: string;
110
+ details?: unknown;
111
+ };
112
+ /**
113
+ * Creates a generic error response body for unknown/unexpected errors.
114
+ * Used as a fallback when error type cannot be determined.
115
+ *
116
+ * @param message - Error message to include in response (defaults to "Internal server error")
117
+ * @returns Standardized error response body with generic INTERNAL_ERROR code
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * import { createGenericErrorResponseBody } from "@uploadista/server";
122
+ *
123
+ * try {
124
+ * // ... operation
125
+ * } catch (err) {
126
+ * const errorResponse = createGenericErrorResponseBody(
127
+ * err instanceof Error ? err.message : "Unknown error"
128
+ * );
129
+ * res.status(500).json(errorResponse);
130
+ * }
131
+ * ```
132
+ */
133
+ export declare const createGenericErrorResponseBody: (message?: string) => {
134
+ error: string;
135
+ code: string;
136
+ timestamp: string;
137
+ };
138
+ //# sourceMappingURL=error-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-types.d.ts","sourceRoot":"","sources":["../src/error-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE/D;;;;;;;;GAQG;AACH,qBAAa,YAAa,SAAQ,KAAK;aAGnB,UAAU,EAAE,MAAM;aAClB,SAAS,EAAE,MAAM;gBAFjC,OAAO,EAAE,MAAM,EACC,UAAU,GAAE,MAAY,EACxB,SAAS,GAAE,MAAyB;CAKvD;AAED;;;;;;;;;;GAUG;AACH,qBAAa,eAAgB,SAAQ,YAAY;gBACnC,OAAO,EAAE,MAAM;CAI5B;AAED;;;;;;;;;;GAUG;AACH,qBAAa,aAAc,SAAQ,YAAY;gBACjC,QAAQ,EAAE,MAAM;CAI7B;AAED;;;;;;;;;;;;;GAaG;AACH,qBAAa,eAAgB,SAAQ,YAAY;gBACnC,OAAO,EAAE,MAAM;CAI5B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,uBAAuB,GAAI,OAAO,YAAY;;;;CAIzD,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,iCAAiC,GAAI,OAAO,eAAe;WAE7D,MAAM;UACP,MAAM;eACD,MAAM;cACP,OAAO;CAYpB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,8BAA8B,GACzC,gBAAiC;;;;CAKjC,CAAC"}
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Base adapter error class for HTTP adapters.
3
+ * All adapter-specific errors should extend this class or one of its subclasses.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * throw new AdapterError("Something went wrong", 500, "INTERNAL_ERROR");
8
+ * ```
9
+ */
10
+ export class AdapterError extends Error {
11
+ statusCode;
12
+ errorCode;
13
+ constructor(message, statusCode = 500, errorCode = "INTERNAL_ERROR") {
14
+ super(message);
15
+ this.statusCode = statusCode;
16
+ this.errorCode = errorCode;
17
+ this.name = "AdapterError";
18
+ }
19
+ }
20
+ /**
21
+ * Validation error - indicates invalid request data or parameters.
22
+ * Returns HTTP 400 Bad Request status.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * if (!isValidUploadId(id)) {
27
+ * throw new ValidationError("Invalid upload ID format");
28
+ * }
29
+ * ```
30
+ */
31
+ export class ValidationError extends AdapterError {
32
+ constructor(message) {
33
+ super(message, 400, "VALIDATION_ERROR");
34
+ this.name = "ValidationError";
35
+ }
36
+ }
37
+ /**
38
+ * Not found error - indicates a requested resource does not exist.
39
+ * Returns HTTP 404 Not Found status.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * if (!upload) {
44
+ * throw new NotFoundError("Upload");
45
+ * }
46
+ * ```
47
+ */
48
+ export class NotFoundError extends AdapterError {
49
+ constructor(resource) {
50
+ super(`${resource} not found`, 404, "NOT_FOUND");
51
+ this.name = "NotFoundError";
52
+ }
53
+ }
54
+ /**
55
+ * Bad request error - indicates a malformed request.
56
+ * Returns HTTP 400 Bad Request status.
57
+ * Similar to ValidationError but for request structure issues.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * try {
62
+ * const data = JSON.parse(body);
63
+ * } catch {
64
+ * throw new BadRequestError("Invalid JSON body");
65
+ * }
66
+ * ```
67
+ */
68
+ export class BadRequestError extends AdapterError {
69
+ constructor(message) {
70
+ super(message, 400, "BAD_REQUEST");
71
+ this.name = "BadRequestError";
72
+ }
73
+ }
74
+ /**
75
+ * Creates a standardized error response object for AdapterError.
76
+ * Includes error message, error code, and ISO timestamp.
77
+ *
78
+ * @param error - The AdapterError to format
79
+ * @returns Standardized error response body
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * import { createErrorResponseBody } from "@uploadista/server";
84
+ *
85
+ * try {
86
+ * // ... operation
87
+ * } catch (err) {
88
+ * const errorResponse = createErrorResponseBody(err);
89
+ * res.status(err.statusCode).json(errorResponse);
90
+ * }
91
+ * ```
92
+ */
93
+ export const createErrorResponseBody = (error) => ({
94
+ error: error.message,
95
+ code: error.errorCode,
96
+ timestamp: new Date().toISOString(),
97
+ });
98
+ /**
99
+ * Creates a standardized error response body from UploadistaError.
100
+ * Formats core library errors for HTTP responses with optional details.
101
+ *
102
+ * @param error - The UploadistaError to format
103
+ * @returns Standardized error response body with error, code, timestamp, and optional details
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * import { createUploadistaErrorResponseBody } from "@uploadista/server";
108
+ *
109
+ * try {
110
+ * const result = yield* uploadServer.handleUpload(input);
111
+ * } catch (err) {
112
+ * if (err instanceof UploadistaError) {
113
+ * const errorResponse = createUploadistaErrorResponseBody(err);
114
+ * res.status(400).json(errorResponse);
115
+ * }
116
+ * }
117
+ * ```
118
+ */
119
+ export const createUploadistaErrorResponseBody = (error) => {
120
+ const response = {
121
+ error: error.body,
122
+ code: error.code,
123
+ timestamp: new Date().toISOString(),
124
+ };
125
+ if (error.details !== undefined) {
126
+ response.details = error.details;
127
+ }
128
+ return response;
129
+ };
130
+ /**
131
+ * Creates a generic error response body for unknown/unexpected errors.
132
+ * Used as a fallback when error type cannot be determined.
133
+ *
134
+ * @param message - Error message to include in response (defaults to "Internal server error")
135
+ * @returns Standardized error response body with generic INTERNAL_ERROR code
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * import { createGenericErrorResponseBody } from "@uploadista/server";
140
+ *
141
+ * try {
142
+ * // ... operation
143
+ * } catch (err) {
144
+ * const errorResponse = createGenericErrorResponseBody(
145
+ * err instanceof Error ? err.message : "Unknown error"
146
+ * );
147
+ * res.status(500).json(errorResponse);
148
+ * }
149
+ * ```
150
+ */
151
+ export const createGenericErrorResponseBody = (message = "Internal server error") => ({
152
+ error: message,
153
+ code: "INTERNAL_ERROR",
154
+ timestamp: new Date().toISOString(),
155
+ });
@@ -0,0 +1,48 @@
1
+ import type { InputFile } from "@uploadista/core/types";
2
+ import type { UploadServerOptions } from "@uploadista/core/upload";
3
+ import type { Context } from "hono";
4
+ export type HonoUploadAdapterOptions = UploadServerOptions;
5
+ export type HonoUploadAdapter = {
6
+ handler: (c: Context) => Promise<Response>;
7
+ upload: (c: Context, file: InputFile, stream: ReadableStream) => Promise<unknown>;
8
+ destroy?: () => Promise<void>;
9
+ };
10
+ /**
11
+ * Create a Hono adapter for the upload server
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const uploadAdapter = createHonoUploadAdapter({
16
+ * dataStore: (storageId) => getDataStore(storageId),
17
+ * kvStore: myKvStore,
18
+ * eventEmitter: myEventEmitter,
19
+ * });
20
+ *
21
+ * const uploadAdapter = { handler: uploadServer.handler };
22
+ *
23
+ * app.on(['POST', 'GET', 'PATCH'], '/api/upload/**', (c) =>
24
+ * uploadAdapter.handler(c)
25
+ * );
26
+ * ```
27
+ */
28
+ export declare const createHonoUploadAdapter: (options: HonoUploadAdapterOptions) => HonoUploadAdapter;
29
+ /**
30
+ * Middleware factory for Hono that handles upload server integration
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const uploadServerMiddleware = createHonoUploadServerMiddleware({
35
+ * dataStore: (storageId) => getDataStore(storageId),
36
+ * kvStore: myKvStore,
37
+ * eventEmitter: myEventEmitter,
38
+ * });
39
+ *
40
+ * app.use(uploadServerMiddleware);
41
+ *
42
+ * app.on(['POST', 'GET', 'PATCH'], '/api/upload/**', (c) =>
43
+ * c.get('uploadServer').handler(c.req.raw)
44
+ * );
45
+ * ```
46
+ */
47
+ export declare const createHonoUploadServerMiddleware: (options: HonoUploadAdapterOptions) => (c: Context, next: () => Promise<void>) => Promise<void>;
48
+ //# sourceMappingURL=hono-adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hono-adapter.d.ts","sourceRoot":"","sources":["../src/hono-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,KAAK,EAEV,mBAAmB,EACpB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC,MAAM,MAAM,wBAAwB,GAAG,mBAAmB,CAAC;AAE3D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,OAAO,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC3C,MAAM,EAAE,CACN,CAAC,EAAE,OAAO,EACV,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,cAAc,KACnB,OAAO,CAAC,OAAO,CAAC,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/B,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,uBAAuB,GAClC,SAAS,wBAAwB,KAChC,iBAqBF,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gCAAgC,GAC3C,SAAS,wBAAwB,MAInB,GAAG,OAAO,EAAE,MAAM,MAAM,OAAO,CAAC,IAAI,CAAC,kBAIpD,CAAC"}
@@ -0,0 +1,58 @@
1
+ import { createFrameworkAgnosticUploadServer } from "@uploadista/core/upload";
2
+ /**
3
+ * Create a Hono adapter for the upload server
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * const uploadAdapter = createHonoUploadAdapter({
8
+ * dataStore: (storageId) => getDataStore(storageId),
9
+ * kvStore: myKvStore,
10
+ * eventEmitter: myEventEmitter,
11
+ * });
12
+ *
13
+ * const uploadAdapter = { handler: uploadServer.handler };
14
+ *
15
+ * app.on(['POST', 'GET', 'PATCH'], '/api/upload/**', (c) =>
16
+ * uploadAdapter.handler(c)
17
+ * );
18
+ * ```
19
+ */
20
+ export const createHonoUploadAdapter = (options) => {
21
+ const server = createFrameworkAgnosticUploadServer(options);
22
+ return {
23
+ handler: async (c) => {
24
+ return server.handler(c.req.raw);
25
+ },
26
+ upload: async (_c, file, stream) => {
27
+ return server.upload(file, stream);
28
+ },
29
+ destroy: async () => {
30
+ await server.destroy?.();
31
+ },
32
+ };
33
+ };
34
+ /**
35
+ * Middleware factory for Hono that handles upload server integration
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * const uploadServerMiddleware = createHonoUploadServerMiddleware({
40
+ * dataStore: (storageId) => getDataStore(storageId),
41
+ * kvStore: myKvStore,
42
+ * eventEmitter: myEventEmitter,
43
+ * });
44
+ *
45
+ * app.use(uploadServerMiddleware);
46
+ *
47
+ * app.on(['POST', 'GET', 'PATCH'], '/api/upload/**', (c) =>
48
+ * c.get('uploadServer').handler(c.req.raw)
49
+ * );
50
+ * ```
51
+ */
52
+ export const createHonoUploadServerMiddleware = (options) => {
53
+ const server = createFrameworkAgnosticUploadServer(options);
54
+ return async (c, next) => {
55
+ c.set("uploadServer", server);
56
+ await next();
57
+ };
58
+ };