Skip to content

Commit

Permalink
feat: pass context to adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojciech Krysiak authored and Wojciech Krysiak committed Sep 7, 2020
1 parent 9ee1b78 commit 4a4a878
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion example-app/src/admin/resources/custom/custom.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Custom } from '../../../custom/custom.entity'

class MyProvider extends BaseProvider {
constructor() {
super('test')
super('bucketName')
}

public async upload() {
Expand Down
2 changes: 1 addition & 1 deletion src/features/upload-file/build-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const buildRemotePath = (
file: UploadedFile,
): string => {
if (!record.id()) {
throw new Error('you cannot upload file for not persisted record. Save record first')
throw new Error('You cannot upload file for not persisted record. Save record first')
}
const { ext, name } = path.parse(file.name)

Expand Down
12 changes: 6 additions & 6 deletions src/features/upload-file/providers/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable class-methods-use-this */

import { UploadedFile } from 'admin-bro'
import { BaseResource, UploadedFile, BaseRecord, ActionContext } from 'admin-bro'
import { UploadOptions } from 'src'

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

/**
*
* @param {string }bucket place where files should be stored
* @param { string } bucket place where files should be stored
*/
constructor(bucket: string) {
this.name = 'BaseAdapter'
Expand All @@ -81,7 +81,7 @@ abstract class BaseProvider {
* @param {string} key file path
* @abstract
*/
public async upload(file: UploadedFile, key: string): Promise<any> {
public async upload(file: UploadedFile, key: string, context: ActionContext): Promise<any> {
throw new Error('you have to implement `BaseProvider#upload` method')
}

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

Expand All @@ -105,7 +105,7 @@ abstract class BaseProvider {
* @async
* @abstract
*/
public path(key: string, bucket: string): Promise<string> | string {
public path(key: string, bucket: string, context: ActionContext): Promise<string> | string {
throw new Error('you have to implement `BaseProvider#path` method')
}
}
Expand Down
28 changes: 16 additions & 12 deletions src/features/upload-file/upload-file.feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import AdminBro, {
RecordJSON,
UploadedFile,
} from 'admin-bro'
import { BulkActionResponse } from 'admin-bro/types/src/backend/actions/action.interface'
import { BulkActionResponse, After } from 'admin-bro/types/src/backend/actions/action.interface'
import buildPath from './build-path'
import AWSProvider from './providers/aws-provider'
import UploadOptions from './upload-options.type'
Expand Down Expand Up @@ -84,7 +84,7 @@ const uploadFileFeature = (config: UploadOptions): FeatureType => {
}

await record.update(params)
await adapter.delete(key, bucket)
await adapter.delete(key, bucket, context)

return {
...response,
Expand All @@ -98,7 +98,7 @@ const uploadFileFeature = (config: UploadOptions): FeatureType => {
const oldRecord = { ...record }
const key = buildPath(record, uploadedFile)

await adapter.upload(uploadedFile, key)
await adapter.upload(uploadedFile, key, context)

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

if (oldKey && oldBucket && (oldKey !== key || oldBucket !== adapter.bucket)) {
await adapter.delete(oldKey, oldBucket)
await adapter.delete(oldKey, oldBucket, context)
}

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

if (record && key) {
const storedBucket = properties.bucket && record.param(properties.bucket)
await adapter.delete(key, storedBucket || adapter.bucket)
await adapter.delete(key, storedBucket || adapter.bucket, context)
}
return response
}
Expand All @@ -156,40 +156,44 @@ const uploadFileFeature = (config: UploadOptions): FeatureType => {
const key = record?.param(properties.key)
if (record && key) {
const storedBucket = properties.bucket && record.param(properties.bucket)
await adapter.delete(key, storedBucket || adapter.bucket)
await adapter.delete(key, storedBucket || adapter.bucket, context)
}
}))

return response
}

const fillRecordWithPath = async (record: RecordJSON): Promise<RecordJSON> => {
const fillRecordWithPath = async (
record: RecordJSON, context: ActionContext,
): Promise<RecordJSON> => {
const key = record?.params[properties.key]
const storedBucket = properties.bucket && record?.params[properties.bucket]

if (key) {
// eslint-disable-next-line no-param-reassign
record.params[filePathProperty] = await adapter.path(key, storedBucket || adapter.bucket)
record.params[filePathProperty] = await adapter.path(
key, storedBucket || adapter.bucket, context,
)
}

return record
}

const fillPath = async (response: RecordActionResponse): Promise<RecordActionResponse> => {
const fillPath: After<RecordActionResponse> = async (response, request, context) => {
const { record } = response

return {
...response,
record: await fillRecordWithPath(record),
record: await fillRecordWithPath(record, context),
}
}

const fillPaths = async (response: ListActionResponse): Promise<ListActionResponse> => {
const fillPaths: After<ListActionResponse> = async (response, request, context) => {
const { records } = response

return {
...response,
records: await Promise.all(records.map(fillRecordWithPath)),
records: await Promise.all(records.map((record) => fillRecordWithPath(record, context))),
}
}

Expand Down

0 comments on commit 4a4a878

Please sign in to comment.