-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtypes.ts
158 lines (134 loc) · 3.57 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* eslint-disable max-classes-per-file */
import { NextToolConfig } from 'next-tool';
import { S3ClientConfig } from '@aws-sdk/client-s3';
import { PresignedPostOptions } from '@aws-sdk/s3-presigned-post';
export type Metadata = Record<string, string | number>;
export type RequiredField<T, K extends keyof T> = T & Required<Pick<T, K>>;
// eslint-disable-next-line no-shadow
export enum NextUploadAction {
deleteAsset = 'deleteAsset',
generatePresignedPostPolicy = 'generatePresignedPostPolicy',
getAsset = 'getAsset',
pruneAssets = 'pruneAssets',
verifyAsset = 'verifyAsset',
}
type CommonConfig = {
includeMetadataInSignedUrlResponse?: boolean;
includeObjectPathInPostPolicyResponse?: boolean;
maxSize?: number | string;
postPolicyExpirationSeconds?: number;
presignedUrlExpirationSeconds?: number;
verifyAssets?: boolean;
verifyAssetsExpirationSeconds?: number;
};
export type UploadTypeConfig = CommonConfig & {
metadata?: Metadata;
path?: string;
postPolicy?: (
postPolicy: PresignedPostOptions
) => Promise<PresignedPostOptions>;
};
export type Asset = {
bucket: string;
createdAt: Date;
expires?: number | null;
fileType: string;
id: string;
metadata: Metadata;
name: string;
path: string;
updatedAt: Date;
uploadType: string;
verified: boolean | null;
};
export interface NextUploadStore {
all(): Promise<Asset[]>;
delete(id: string): Promise<void>;
deletePresignedUrl(id: string): Promise<void>;
find(id: string): Promise<Asset | undefined>;
getPresignedUrl(id: string): Promise<{
presignedUrl?: string | null;
presignedUrlExpires?: number | null;
} | null>;
savePresignedUrl(
id: string,
url: string,
presignedUrlExpirationSeconds?: number
): Promise<void>;
upsert(args: Asset, ttl: number): Promise<Asset>;
}
type ClientConfig = S3ClientConfig;
export type UploadTypeConfigFn = (
args: Partial<GeneratePresignedPostPolicyArgs & GetAssetArgs>,
request?: NextUploadRequest
) => Promise<UploadTypeConfig>;
export type NextUploadConfig = NextToolConfig &
RequiredField<CommonConfig, 'maxSize'> & {
api?: string;
bucket?: string;
client: ClientConfig;
uploadTypes?: {
[uploadType: string]: UploadTypeConfigFn | UploadTypeConfig;
};
};
export type NextUploadClientConfig = Pick<NextUploadConfig, 'api'>;
export type NextUploadRequest = {
body?: any;
headers?: Headers;
};
export type GeneratePresignedPostPolicyArgs = {
fileType?: string;
id?: string;
metadata?: any;
name?: string;
uploadType?: string;
};
export type SaveUploadArgs = GeneratePresignedPostPolicyArgs;
export interface Storage {
saveUpload(args: SaveUploadArgs): Promise<string>;
}
export type GeneratePresignedPostPolicyOptions = {
args?: GeneratePresignedPostPolicyArgs;
requestInit?: any;
};
export type GetAssetOptions = {
args?: GetAssetArgs;
requestInit?: any;
};
export type UploadToPresignedUrlOptions = {
file: File;
formData?: FormData;
metadata?: Metadata;
postPolicy: SignedPostPolicy;
requestInit?: any;
};
export type SignedPostPolicy = {
data: any;
id: string;
path: string | null;
url: string;
};
export type UploadOptions = GeneratePresignedPostPolicyArgs & {
file: File;
requestInit?: any;
};
export type GetAssetArgs = {
expiry?: number;
id?: string;
path?: string | null;
reqParams?: { [key: string]: any };
requestDate?: Date;
};
export type GetAsset = {
id: string;
metadata?: Metadata | null;
url: string;
};
export type VerifyAssetArgs = {
id?: string;
path?: string;
};
export type DeleteArgs = {
id?: string;
path?: string | null;
};