-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": [ | ||
"../../.eslintrc.json" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/src/** | ||
tsconfig*.json | ||
.eslintrc.json | ||
typedoc.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Logion Client SDK (Expo) | ||
|
||
This project provides a JS/TypeScript SDK enabling an [Expo](https://expo.dev) | ||
application to interact with a logion network. | ||
|
||
## Installation | ||
|
||
Use your favorite package manager (e.g. yarn) and add `@logion/client` and `@logion/client-expo` packages to your project. | ||
you will also need to install some peer dependencies: `buffer`, `expo-file-system` and `expo-crypto`. | ||
|
||
You may have to setup `buffer` by adding the following line as soon as possible in your code: | ||
|
||
```js | ||
global.Buffer = Buffer; | ||
``` | ||
|
||
## Usage | ||
|
||
Instantiate the Logion client using `newLogionClient` function. For example, to connect to our test environment use: | ||
|
||
```typescript | ||
import { Environment } from "@logion/client"; | ||
import { newLogionClient } from '@logion/client-expo'; | ||
|
||
const client = await newLogionClient(Environment.TEST); | ||
``` | ||
|
||
See [here](../client/README.md) for instructions about how to use the client. | ||
|
||
Each time you need to pass a file to the client (e.g. when creating a collection item or a tokens record), | ||
pass an instance of `ExpoFile`. The constructor expects 3 arguments: | ||
|
||
- the [URI](https://docs.expo.dev/versions/latest/sdk/filesystem/#api) of the file, | ||
- its name (e.g. `my-document.pdf`), | ||
- its MIME type (e.g. `application/pdf`). | ||
|
||
Here is an example: | ||
|
||
```typescript | ||
// ... | ||
import { MimeType } from "@logion/client"; | ||
import { ExpoFile } from '@logion/client-expo'; | ||
// ... | ||
|
||
const fileName = "file.txt"; | ||
const file = new ExpoFile(`${FileSystem.cacheDirectory}/${fileName}`, fileName, MimeType.from("text/plain")); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "@logion/client-expo", | ||
"version": "0.1.0-1", | ||
"description": "logion SDK for client applications built with Expo", | ||
"main": "dist/index.js", | ||
"packageManager": "[email protected]", | ||
"type": "module", | ||
"scripts": { | ||
"build": "yarn lint && tsc -p tsconfig.json", | ||
"lint": "yarn eslint src/**", | ||
"test": "echo 'Nothing to test for now'", | ||
"clean": "rm -rf dist" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/logion-network/logion-api.git", | ||
"directory": "packages/client-expo" | ||
}, | ||
"keywords": [ | ||
"logion", | ||
"api", | ||
"client", | ||
"expo" | ||
], | ||
"author": "Logion Team", | ||
"license": "Apache-2.0", | ||
"peerDependencies": { | ||
"@logion/client": "0.x", | ||
"expo-crypto": "12.x", | ||
"expo-file-system": "16.x" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/logion-network/logion-api/issues" | ||
}, | ||
"homepage": "https://github.com/logion-network/logion-api/packages/client-expo#readme", | ||
"devDependencies": { | ||
"@logion/client": "workspace:^", | ||
"@tsconfig/node18": "^1.0.1", | ||
"@types/node": "^18.6.1", | ||
"@typescript-eslint/eslint-plugin": "^6.9.1", | ||
"@typescript-eslint/parser": "^6.9.1", | ||
"eslint": "^8.20.0", | ||
"expo-crypto": "^12.8.1", | ||
"expo-file-system": "^16.0.7", | ||
"typescript": "^5.2.2" | ||
}, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"stableVersion": "0.1.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
AxiosFileUploader, | ||
createLogionClientConfig as createConfig, | ||
Environment, | ||
EnvironmentString, | ||
File, | ||
FormDataLike, | ||
HashAndSize, | ||
LogionClient, | ||
MimeType, | ||
} from "@logion/client"; | ||
import { Hash } from "@logion/node-api"; | ||
import * as Crypto from 'expo-crypto'; | ||
import * as FileSystem from "expo-file-system"; | ||
|
||
export async function newLogionClient(env: Environment | EnvironmentString): Promise<LogionClient> { | ||
return await LogionClient.create(createConfig(env, () => new ExpoFileUploader())) | ||
} | ||
|
||
export class ExpoFile extends File { | ||
|
||
constructor(uri: string, name: string, mimeType: MimeType) { | ||
super(name, mimeType); | ||
this.uri = uri; | ||
} | ||
|
||
private uri: string; | ||
|
||
async getHashAndSize(): Promise<HashAndSize> { | ||
const fileInfo = await FileSystem.getInfoAsync(this.uri); | ||
if(!fileInfo.exists) { | ||
throw new Error("File does not exist"); | ||
} else { | ||
const base64Data = await FileSystem.readAsStringAsync(this.uri, { encoding: FileSystem.EncodingType.Base64 }); | ||
const binaryData = Buffer.from(base64Data, 'base64'); | ||
const hash = await Crypto.digest(Crypto.CryptoDigestAlgorithm.SHA256, binaryData); | ||
return { | ||
size: BigInt(fileInfo.size), | ||
hash: new Hash(new Uint8Array(hash)), | ||
}; | ||
} | ||
} | ||
|
||
getPath(): string { | ||
return this.uri; | ||
} | ||
} | ||
|
||
export class ExpoFileUploader extends AxiosFileUploader { | ||
|
||
buildFormData(): FormDataLike { | ||
return new FormData(); | ||
} | ||
|
||
async toFormDataValue(file: File): Promise<{ uri: string, type: string, name: string }> { | ||
const reactNativeFile = this.ensureExpo(file); | ||
return { | ||
uri: `file://${ reactNativeFile.getPath() }`, | ||
type: file.mimeType.mimeType, | ||
name: file.name, | ||
}; | ||
} | ||
|
||
private ensureExpo(file: File): ExpoFile { | ||
if(file instanceof ExpoFile) { | ||
return file; | ||
} else { | ||
throw new Error("Unexpected file type"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"lib": ["dom"], | ||
"baseUrl": "." | ||
}, | ||
"include": [ | ||
"./src/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "client-expo", | ||
"entryPoints": [ | ||
"./src/index.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters