Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- new event `import-data` is available

## [1.0.10] - 2023-06-22

- new events `fetch-files`, `fetched-files` are available
Expand Down
1 change: 1 addition & 0 deletions base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export declare type HttpClientInstance = {
put: PostHandler
delete: PostHandler
postMultipart: PostMultipartHandler
patchMultipart: PatchMultipartHandler,
fetch: FetchHandler
}

Expand Down
12 changes: 12 additions & 0 deletions src/events/bk/importData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {factory, Payload} from '../factory'

export type ImportDataPayload = Payload

/**
* @registeredEvent
* @title Import Data
* @description raised when the import button is clicked
* @payload {
* }
*/
export const importData = factory<ImportDataPayload>('import-data')
1 change: 1 addition & 0 deletions src/events/bk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './fetchedFiles'
export * from './fetchFiles'
export * from './filter'
export * from './formSubmit'
export * from './importData'
export * from './initPlugin'
export * from './httpDelete'
export * from './layoutChange'
Expand Down
37 changes: 31 additions & 6 deletions src/utils/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type PostMultipartHandler =
<T = any>(url: string, data: FormData, config?: HttpClientConfig) =>
Promise<HttpClientResponse<T>>

type PatchMultipartHandler =
<T = any>(url: string, data: FormData, config?: HttpClientConfig) =>
Promise<HttpClientResponse<T>>

export type HttpMethods = 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE'

export interface HttpClientSupport extends Element {
Expand All @@ -45,6 +49,7 @@ export type HttpClientInstance = {
patch: WithBodyHandler
delete: WithBodyHandler
postMultipart: PostMultipartHandler
patchMultipart: PatchMultipartHandler
fetch: FetchHandler
}

Expand Down Expand Up @@ -217,26 +222,45 @@ const withBody = (method: 'POST' | 'DELETE' | 'PUT' | 'PATCH') =>
}
}

async function postMultipart (
async function postMultipart(
this: HttpClientSupport,
path: string,
data: FormData,
config?: HttpClientConfig
): Promise<HttpClientResponse<any>> {
return await executeMultipart(this, 'POST', path, data, config)
}

async function patchMultipart(
this: HttpClientSupport,
path: string,
data: FormData,
config?: HttpClientConfig
): Promise<HttpClientResponse<any>> {
return await executeMultipart(this, 'PATCH', path, data, config)
}

async function executeMultipart (
httpClient: HttpClientSupport,
method: 'POST' | 'PATCH',
path: string,
data: FormData,
config?: HttpClientConfig
): Promise<HttpClientResponse<any>> {
try {
const {proxyWindow = window} = this
const {proxyWindow = window} = httpClient
const [url, {
outputTransform, ...fetchConfig
}] = initParams(path, this.basePath, config)
}] = initParams(path, httpClient.basePath, config)

const fetchPromise = proxyWindow.fetch(url.toString(), {
...fetchConfig,
method: 'POST',
method,
body: data,
headers: {
...this.headers ?? {}, ...fetchConfig.headers ?? {}
...httpClient.headers ?? {}, ...fetchConfig.headers ?? {}
},
credentials: fetchConfig.credentials ?? this.credentials,
credentials: fetchConfig.credentials ?? httpClient.credentials,
})

if (fetchConfig.raw) {
Expand Down Expand Up @@ -285,6 +309,7 @@ export function createFetchHttpClient (this: HttpClientSupport): HttpClientInsta
put: withBody('PUT').bind<WithBodyHandler>(this),
patch: withBody('PATCH').bind<WithBodyHandler>(this),
postMultipart: postMultipart.bind<PostMultipartHandler>(this),
patchMultipart: patchMultipart.bind<PatchMultipartHandler>(this),
fetch: _fetch.bind<FetchHandler>(this)
}
}