Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: monkey patch bee js get ky #150

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 0 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { downloadData, generateBlockName } from './handler'
import { blocksToManifest, getFileMetadataRawBytes, rawFileMetadataToFileMetadata } from './adapter'
import { Blocks, DataUploadOptions, FileReceiveOptions, FileShareInfo } from './types'
import { addEntryToDirectory, removeEntryFromDirectory } from '../content-items/handler'
import { Data, Reference } from '@ethersphere/bee-js'
import { Bee, BeeError, Data, KyRequestOptions, Reference, RequestOptions } from '@ethersphere/bee-js'
import { getRawMetadata } from '../content-items/utils'
import { assertRawFileMetadata, combine } from '../directory/utils'
import { assertEncryptedReference, EncryptedReference } from '../utils/hex'
Expand Down Expand Up @@ -67,6 +67,12 @@ export class File {
data: Uint8Array | string,
options?: DataUploadOptions,
): Promise<FileMetadata> {
// @ts-ignore
const cloneGetKy = Bee.prototype.getKy

// @ts-ignore
Bee.prototype.getKy = this.fetchPolyfill(this.accountData.connection.bee.url)

options = { ...this.defaultUploadOptions, ...options }
assertAccount(this.accountData)
assertPodName(podName)
Expand Down Expand Up @@ -112,6 +118,8 @@ export class File {
await addEntryToDirectory(connection, extendedInfo.podWallet, pathInfo.path, pathInfo.filename, true)
await writeFeedData(connection, fullPath, getFileMetadataRawBytes(meta), extendedInfo.podWallet.privateKey)

// @ts-ignore
Bee.prototype.getKy = cloneGetKy
return meta
}

Expand Down Expand Up @@ -201,4 +209,25 @@ export class File {

return meta
}

/**
* fetch polyfill for ky and bee-js
* @param options Options that affects the request behavior
*/
fetchPolyfill(beeUrl: string) {
return (options: RequestOptions = {}): any => {
return async (url: any, kyOpts: KyRequestOptions): Promise<any> => {
const _url = `${beeUrl}/${url}`
const res = await fetch(_url, {
...kyOpts,
...options,
headers: Object(kyOpts.headers),
})
return {
data: undefined,
...res,
} // KyResponse type
}
}
}
}