@uplift-io/uplift 1.0.0 → 1.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.
@@ -15,7 +15,13 @@ type UploadedFile = {
15
15
  size: number;
16
16
  extension?: string | undefined;
17
17
  provider: string;
18
+ outputs?: Record<string, UploadedFile> | undefined;
18
19
  };
20
+ type ClientUploadedFile<TOutputNames extends string = never> = UploadedFile & ([
21
+ TOutputNames
22
+ ] extends [never] ? object : {
23
+ output<TName extends TOutputNames>(name: TName): UploadedFile;
24
+ });
19
25
  type UploadErrorCode = "FILE_TOO_LARGE" | "FILE_TOO_SMALL" | "INVALID_TYPE" | "AUTH_FAILED" | "VALIDATION_FAILED" | "UPLOAD_FAILED" | "UNKNOWN";
20
26
  declare class UploadError extends Error {
21
27
  readonly code: UploadErrorCode;
@@ -53,11 +59,34 @@ type StoragePutInput = {
53
59
  type StorageAdapter = {
54
60
  provider: string;
55
61
  put(input: StoragePutInput): Promise<UploadedFile>;
62
+ delete?(key: string): Promise<void>;
56
63
  };
57
64
  type Middleware<TUser = unknown> = (ctx: {
58
65
  req: Request;
59
66
  }) => TUser | Promise<TUser>;
60
67
  type UploadKind = "any" | "image" | "pdf" | "video" | "audio" | "text" | "json" | "csv" | "custom";
68
+ type PreparedUploadFile = {
69
+ file: UploadInputFile;
70
+ body: File;
71
+ };
72
+ type TransformContext = PreparedUploadFile;
73
+ type UploadTransform<TKind extends UploadKind = UploadKind> = {
74
+ readonly __kind?: TKind | undefined;
75
+ transform(ctx: TransformContext): File | PreparedUploadFile | Promise<File | PreparedUploadFile>;
76
+ };
77
+ type UploadTransformFunction<TKind extends UploadKind = UploadKind> = ((ctx: TransformContext) => File | PreparedUploadFile | Promise<File | PreparedUploadFile>) & {
78
+ readonly __kind?: TKind | undefined;
79
+ };
80
+ type CompatibleTransform<TKind extends UploadKind> = TKind extends "any" | "custom" ? UploadTransform<UploadKind> | UploadTransformFunction<UploadKind> : UploadTransform<TKind> | UploadTransform<"any"> | UploadTransformFunction<TKind> | UploadTransformFunction<"any">;
81
+ type OutputContext = PreparedUploadFile & {
82
+ primary: UploadedFile;
83
+ };
84
+ type UploadOutput<TKind extends UploadKind = UploadKind, TName extends string = string> = {
85
+ readonly __kind?: TKind | undefined;
86
+ name: TName;
87
+ produce(ctx: OutputContext): File | PreparedUploadFile | Promise<File | PreparedUploadFile>;
88
+ };
89
+ type CompatibleOutput<TKind extends UploadKind, TName extends string = string> = TKind extends "any" | "custom" ? UploadOutput<UploadKind, TName> : UploadOutput<TKind, TName> | UploadOutput<"any", TName>;
61
90
  type UploadRouteDefinition = {
62
91
  kind: UploadKind;
63
92
  maxBytes?: number;
@@ -102,6 +131,8 @@ type UploadRouteDefinition = {
102
131
  min?: DurationValue;
103
132
  max?: DurationValue;
104
133
  };
134
+ transforms?: Array<UploadTransform | UploadTransformFunction>;
135
+ outputs?: Array<UploadOutput>;
105
136
  };
106
137
  type UploadRoutes = Record<string, {
107
138
  _def: UploadRouteDefinition;
@@ -119,10 +150,13 @@ type UpliftApp<TRoutes extends UploadRoutes = UploadRoutes> = {
119
150
  type IsMultiple<TRoute> = TRoute extends {
120
151
  __multiple?: infer TMultiple;
121
152
  } ? TMultiple extends true ? true : false : false;
153
+ type OutputNames<TRoute> = TRoute extends {
154
+ __outputs?: infer TOutputNames;
155
+ } ? TOutputNames extends string ? TOutputNames : never : never;
122
156
  type ClientInput<TRoute> = IsMultiple<TRoute> extends true ? File[] | FileList : File;
123
- type ClientOutput<TRoute> = IsMultiple<TRoute> extends true ? UploadedFile[] : UploadedFile;
157
+ type ClientOutput<TRoute> = IsMultiple<TRoute> extends true ? Array<ClientUploadedFile<OutputNames<TRoute>>> : ClientUploadedFile<OutputNames<TRoute>>;
124
158
  type UploadClient<TApp extends UpliftApp> = {
125
159
  [TRouteName in keyof TApp["routes"] & string]: (input: ClientInput<TApp["routes"][TRouteName]>) => Promise<ClientOutput<TApp["routes"][TRouteName]>>;
126
160
  };
127
161
 
128
- export { type ClientInput as C, type DurationValue as D, type KeyContext as K, type Middleware as M, type SizeValue as S, type UploadKind as U, type UploadRouteDefinition as a, type UploadInputFile as b, type DoneContext as c, type StandardSchema as d, type UploadRoutes as e, type StorageAdapter as f, type UpliftApp as g, type ClientOutput as h, type StoragePutInput as i, type UploadClient as j, UploadError as k, type UploadErrorCode as l, type UploadedFile as m };
162
+ export { type CompatibleTransform as C, type DurationValue as D, type KeyContext as K, type Middleware as M, type OutputContext as O, type PreparedUploadFile as P, type SizeValue as S, type TransformContext as T, type UploadKind as U, type UploadRouteDefinition as a, type UploadInputFile as b, type DoneContext as c, type StandardSchema as d, type CompatibleOutput as e, type UploadRoutes as f, type StorageAdapter as g, type UpliftApp as h, type ClientInput as i, type ClientOutput as j, type ClientUploadedFile as k, type OutputNames as l, type StoragePutInput as m, type UploadClient as n, UploadError as o, type UploadErrorCode as p, type UploadOutput as q, type UploadTransform as r, type UploadTransformFunction as s, type UploadedFile as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uplift-io/uplift",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Dead-simple, type-safe file handling for TypeScript applications.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://itzfeminisce.github.io/uplift/",