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

Api bzz react native #98

Merged
merged 15 commits into from
Aug 8, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.log
/.idea
/.vscode
/packages/*/cjs
/packages/*/dist
/packages/*/esm
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ npm install -g @erebos/cli
| **Individual APIs**
| [`@erebos/api-bzz-browser`](/packages/api-bzz-browser) | [![npm version](https://img.shields.io/npm/v/@erebos/api-bzz-browser.svg)](https://www.npmjs.com/package/@erebos/api-bzz-browser) | Browser-only Swarm (BZZ) APIs
| [`@erebos/api-bzz-node`](/packages/api-bzz-node) | [![npm version](https://img.shields.io/npm/v/@erebos/api-bzz-node.svg)](https://www.npmjs.com/package/@erebos/api-bzz-node) | Node-only Swarm (BZZ) APIs
| [`@erebos/api-bzz-react-native`](/packages/api-bzz-react-native) | [![npm version](https://img.shields.io/npm/v/@erebos/api-bzz-react-native.svg)](https://www.npmjs.com/package/@erebos/api-bzz-node) | Experimental React Native Swarm (BZZ) APIs
| [`@erebos/api-pss`](/packages/api-pss) | [![npm version](https://img.shields.io/npm/v/@erebos/api-pss.svg)](https://www.npmjs.com/package/@erebos/api-pss) | Postal Services over Swarm (PSS) APIs
| [`@erebos/timeline`](/packages/timeline) | [![npm version](https://img.shields.io/npm/v/@erebos/timeline.svg)](https://www.npmjs.com/package/@erebos/timeline) | Feed-based Timeline APIs
| **Utilities**
Expand Down
1 change: 1 addition & 0 deletions packages/api-bzz-react-native/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "../../.babelrc" }
33 changes: 33 additions & 0 deletions packages/api-bzz-react-native/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@erebos/api-bzz-react-native",
"version": "0.9.0",
"description": "Bzz API for react-native",
"repository": "git@github.com:MainframeHQ/erebos.git",
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "types/index.d.ts",
"author": "Mainframe",
"license": "MIT",
"files": [
"cjs/*",
"esm/*",
"types/*"
],
"scripts": {
"clean": "del cjs esm types",
"build:cjs": "BABEL_ENV='browser-cjs' babel src --out-dir cjs --extensions \".ts\"",
"build:esm": "BABEL_ENV='browser-esm' babel src --out-dir esm --extensions \".ts\"",
"build:js": "yarn build:cjs && yarn build:esm",
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build": "yarn clean && yarn build:js && yarn build:types",
"prepublishOnly": "yarn build"
},
"dependencies": {
"@babel/runtime": "^7.5.5",
"@erebos/api-bzz-base": "^0.9.0",
"universal-url": "^2.0.0"
},
"devDependencies": {
"typescript": "3.5.2"
}
}
52 changes: 52 additions & 0 deletions packages/api-bzz-react-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-env browser */
// @ts-ignore
import { URL } from 'universal-url'
import {
BaseBzz,
BzzConfig,
DirectoryData,
UploadOptions,
} from '@erebos/api-bzz-base'
import { hexValue } from '@erebos/hex'

export * from '@erebos/api-bzz-base'

export class Bzz extends BaseBzz<Response> {
public constructor(config: BzzConfig) {
const { url, ...cfg } = config
super(window.fetch.bind(window), { ...cfg, url: new URL(url).href })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use global instead of window?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is needed, fetch is available globally in react-native, should we just pass in fetch?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure in RN but on the Web it needed to be bound to the proper context, there was some issue without.

}

public uploadDirectory(
directory: DirectoryData,
options: UploadOptions = {},
): Promise<hexValue> {
const form = new FormData()
Object.keys(directory).forEach(key => {
form.append(
key,
{
uri: directory[key].data,
type: directory[key].contentType,
name: key,
} as any,
key,
)
})
if (options.defaultPath != null) {
const file = directory[options.defaultPath]
if (file != null) {
form.append(
'',
{
uri: directory[options.defaultPath].data,
type: directory[options.defaultPath].contentType,
name: options.defaultPath,
} as any,
'',
)
}
}
return this.uploadBody(form, options)
}
}
8 changes: 8 additions & 0 deletions packages/api-bzz-react-native/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"lib": ["dom", "es2015"],
"outDir": "./types"
},
"include": ["./src/**/*"]
}
7 changes: 7 additions & 0 deletions packages/api-bzz-react-native/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseBzz, BzzConfig, DirectoryData, UploadOptions } from '@erebos/api-bzz-base';
import { hexValue } from '@erebos/hex';
export * from '@erebos/api-bzz-base';
export declare class Bzz extends BaseBzz<Response> {
constructor(config: BzzConfig);
uploadDirectory(directory: DirectoryData, options?: UploadOptions): Promise<hexValue>;
}
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": true,
"lib": ["es2015"],
"lib": ["es2015", "dom"],
"moduleResolution": "node",
"strict": true,
"target": "esnext"
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4910,6 +4910,11 @@ hash.js@^1.0.0, hash.js@^1.0.3:
inherits "^2.0.3"
minimalistic-assert "^1.0.1"

hasurl@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/hasurl/-/hasurl-1.0.0.tgz#e4c619097ae1e8fc906bee904ce47e94f5e1ea37"
integrity sha512-43ypUd3DbwyCT01UYpA99AEZxZ4aKtRxWGBHEIbjcOsUghd9YUON0C+JF6isNjaiwC/UF5neaUudy6JS9jZPZQ==

hmac-drbg@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"
Expand Down Expand Up @@ -9007,6 +9012,14 @@ unique-slug@^2.0.0:
dependencies:
imurmurhash "^0.1.4"

universal-url@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/universal-url/-/universal-url-2.0.0.tgz#35e7fc2c3374804905cee67ea289ed3a47669809"
integrity sha512-3DLtXdm/G1LQMCnPj+Aw7uDoleQttNHp2g5FnNQKR6cP6taNWS1b/Ehjjx4PVyvejKi3TJyu8iBraKM4q3JQPg==
dependencies:
hasurl "^1.0.0"
whatwg-url "^7.0.0"

universal-user-agent@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-2.1.0.tgz#5abfbcc036a1ba490cb941f8fd68c46d3669e8e4"
Expand Down