Skip to content

Commit 4a4a878

Browse files
Wojciech KrysiakWojciech Krysiak
authored andcommitted
feat: pass context to adapter
1 parent 9ee1b78 commit 4a4a878

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

example-app/src/admin/resources/custom/custom.resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Custom } from '../../../custom/custom.entity'
77

88
class MyProvider extends BaseProvider {
99
constructor() {
10-
super('test')
10+
super('bucketName')
1111
}
1212

1313
public async upload() {

src/features/upload-file/build-path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const buildRemotePath = (
1616
file: UploadedFile,
1717
): string => {
1818
if (!record.id()) {
19-
throw new Error('you cannot upload file for not persisted record. Save record first')
19+
throw new Error('You cannot upload file for not persisted record. Save record first')
2020
}
2121
const { ext, name } = path.parse(file.name)
2222

src/features/upload-file/providers/base-provider.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
/* eslint-disable @typescript-eslint/ban-ts-comment */
33
/* eslint-disable class-methods-use-this */
44

5-
import { UploadedFile } from 'admin-bro'
5+
import { BaseResource, UploadedFile, BaseRecord, ActionContext } from 'admin-bro'
6+
import { UploadOptions } from 'src'
67

78
/**
89
* Abstract class which is a base for every @admin-bro/upload Adapter.
@@ -66,8 +67,7 @@ abstract class BaseProvider {
6667
public bucket: string
6768

6869
/**
69-
*
70-
* @param {string }bucket place where files should be stored
70+
* @param { string } bucket place where files should be stored
7171
*/
7272
constructor(bucket: string) {
7373
this.name = 'BaseAdapter'
@@ -81,7 +81,7 @@ abstract class BaseProvider {
8181
* @param {string} key file path
8282
* @abstract
8383
*/
84-
public async upload(file: UploadedFile, key: string): Promise<any> {
84+
public async upload(file: UploadedFile, key: string, context: ActionContext): Promise<any> {
8585
throw new Error('you have to implement `BaseProvider#upload` method')
8686
}
8787

@@ -92,7 +92,7 @@ abstract class BaseProvider {
9292
* @param {string} bucket where file should be uploaded
9393
* @abstract
9494
*/
95-
public async delete(key: string, bucket: string): Promise<any> {
95+
public async delete(key: string, bucket: string, context: ActionContext): Promise<any> {
9696
throw new Error('you have to implement `BaseProvider#delete` method')
9797
}
9898

@@ -105,7 +105,7 @@ abstract class BaseProvider {
105105
* @async
106106
* @abstract
107107
*/
108-
public path(key: string, bucket: string): Promise<string> | string {
108+
public path(key: string, bucket: string, context: ActionContext): Promise<string> | string {
109109
throw new Error('you have to implement `BaseProvider#path` method')
110110
}
111111
}

src/features/upload-file/upload-file.feature.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import AdminBro, {
88
RecordJSON,
99
UploadedFile,
1010
} from 'admin-bro'
11-
import { BulkActionResponse } from 'admin-bro/types/src/backend/actions/action.interface'
11+
import { BulkActionResponse, After } from 'admin-bro/types/src/backend/actions/action.interface'
1212
import buildPath from './build-path'
1313
import AWSProvider from './providers/aws-provider'
1414
import UploadOptions from './upload-options.type'
@@ -84,7 +84,7 @@ const uploadFileFeature = (config: UploadOptions): FeatureType => {
8484
}
8585

8686
await record.update(params)
87-
await adapter.delete(key, bucket)
87+
await adapter.delete(key, bucket, context)
8888

8989
return {
9090
...response,
@@ -98,7 +98,7 @@ const uploadFileFeature = (config: UploadOptions): FeatureType => {
9898
const oldRecord = { ...record }
9999
const key = buildPath(record, uploadedFile)
100100

101-
await adapter.upload(uploadedFile, key)
101+
await adapter.upload(uploadedFile, key, context)
102102

103103
const params = {
104104
[properties.key]: key,
@@ -116,7 +116,7 @@ const uploadFileFeature = (config: UploadOptions): FeatureType => {
116116
) || adapter.bucket
117117

118118
if (oldKey && oldBucket && (oldKey !== key || oldBucket !== adapter.bucket)) {
119-
await adapter.delete(oldKey, oldBucket)
119+
await adapter.delete(oldKey, oldBucket, context)
120120
}
121121

122122
return {
@@ -140,7 +140,7 @@ const uploadFileFeature = (config: UploadOptions): FeatureType => {
140140

141141
if (record && key) {
142142
const storedBucket = properties.bucket && record.param(properties.bucket)
143-
await adapter.delete(key, storedBucket || adapter.bucket)
143+
await adapter.delete(key, storedBucket || adapter.bucket, context)
144144
}
145145
return response
146146
}
@@ -156,40 +156,44 @@ const uploadFileFeature = (config: UploadOptions): FeatureType => {
156156
const key = record?.param(properties.key)
157157
if (record && key) {
158158
const storedBucket = properties.bucket && record.param(properties.bucket)
159-
await adapter.delete(key, storedBucket || adapter.bucket)
159+
await adapter.delete(key, storedBucket || adapter.bucket, context)
160160
}
161161
}))
162162

163163
return response
164164
}
165165

166-
const fillRecordWithPath = async (record: RecordJSON): Promise<RecordJSON> => {
166+
const fillRecordWithPath = async (
167+
record: RecordJSON, context: ActionContext,
168+
): Promise<RecordJSON> => {
167169
const key = record?.params[properties.key]
168170
const storedBucket = properties.bucket && record?.params[properties.bucket]
169171

170172
if (key) {
171173
// eslint-disable-next-line no-param-reassign
172-
record.params[filePathProperty] = await adapter.path(key, storedBucket || adapter.bucket)
174+
record.params[filePathProperty] = await adapter.path(
175+
key, storedBucket || adapter.bucket, context,
176+
)
173177
}
174178

175179
return record
176180
}
177181

178-
const fillPath = async (response: RecordActionResponse): Promise<RecordActionResponse> => {
182+
const fillPath: After<RecordActionResponse> = async (response, request, context) => {
179183
const { record } = response
180184

181185
return {
182186
...response,
183-
record: await fillRecordWithPath(record),
187+
record: await fillRecordWithPath(record, context),
184188
}
185189
}
186190

187-
const fillPaths = async (response: ListActionResponse): Promise<ListActionResponse> => {
191+
const fillPaths: After<ListActionResponse> = async (response, request, context) => {
188192
const { records } = response
189193

190194
return {
191195
...response,
192-
records: await Promise.all(records.map(fillRecordWithPath)),
196+
records: await Promise.all(records.map((record) => fillRecordWithPath(record, context))),
193197
}
194198
}
195199

0 commit comments

Comments
 (0)