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

Multi chain connector #219

Merged
merged 9 commits into from
Jan 26, 2025
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
3 changes: 2 additions & 1 deletion examples/auto-drive-create-next-app/export.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createAutoDriveApi, uploadFolderFromFolderPath } from '@autonomys/auto-drive'
import { NetworkId } from '@autonomys/auto-utils'
import path from 'path'
import { fileURLToPath } from 'url'

Expand All @@ -16,7 +17,7 @@ async function uploadBuild() {
process.exit(1)
}

const api = createAutoDriveApi({ apiKey })
const api = createAutoDriveApi({ apiKey, network: NetworkId.TAURUS })
const outDir = path.join(__dirname, 'out')

try {
Expand Down
18 changes: 12 additions & 6 deletions packages/auto-drive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ Here is an example of how to use the `uploadFileFromFilepath` method to upload a

```typescript
import { uploadFileFromFilepath,createAutoDriveApi } from '@autonomys/auto-drive'
import { NetworkId } from '@autonomys/auto-utils'

const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
const filePath = 'path/to/your/file.txt' // Specify the path to your file
const options = {
password: 'your-encryption-password', // Optional: specify a password for encryption
Expand All @@ -54,8 +55,9 @@ console.log(`The file is uploaded and its cid is ${cid}`)

```typescript
import { uploadFileFromInput, createAutoDriveApi } from '@autonomys/auto-drive'
import { NetworkId } from '@autonomys/auto-utils'

const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key

// e.g Get File from object from HTML event
const file: File = e.target.value // Substitute with your file
Expand Down Expand Up @@ -88,8 +90,9 @@ You could upload any file that could be represented in that way. For example, up

```typescript
import { createAutoDriveApi, uploadFile } from '@autonomys/auto-drive'
import { NetworkId } from '@autonomys/auto-utils'

const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
const buffer = Buffer.from(...);
const genericFile = {
read: async function *() {
Expand Down Expand Up @@ -119,8 +122,9 @@ console.log(`The file is uploaded and its cid is ${cid}`)

```ts
import { createAutoDriveApi, uploadFolderFromFolderPath } from '@autonomys/auto-drive'
import { NetworkId } from '@autonomys/auto-utils'

const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key
const folderPath = 'path/to/your/folder' // Specify the path to your folder

const options = {
Expand All @@ -145,8 +149,9 @@ Here is an example of how to use the `downloadFile` method to download a file fr

```typescript
import { createAutoDriveApi, downloadFile } from '@autonomys/auto-drive'
import { NetworkId } from '@autonomys/auto-utils'

const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key

try {
const cid = '..'
Expand All @@ -167,8 +172,9 @@ Here is an example of how to use the `getRoots` method to retrieve the root dire

```typescript
import { createAutoDriveApi, apiCalls, Scope } from '@autonomys/auto-drive'
import { NetworkId } from '@autonomys/auto-utils'

const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
const api = createAutoDriveApi({ apiKey: 'your-api-key', network: NetworkId.TAURUS }) // Initialize your API instance with API key

try {
const myFiles = await apiCalls.getRoots(api, {
Expand Down
30 changes: 23 additions & 7 deletions packages/auto-drive/src/api/connection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { NetworkId } from '@autonomys/auto-utils'
import { AutoDriveNetwork, getNetworkUrl, networks } from './networks'

export interface AutoDriveApi {
sendRequest: (
relativeUrl: string,
Expand All @@ -14,15 +17,28 @@ export enum OAuthProvider {
export type ApiKeyAuthProvider = 'apikey'
export type AuthProvider = ApiKeyAuthProvider | 'oauth'

type ConnectionOptions =
| {
provider?: AuthProvider
apiKey?: string
url?: null
network: AutoDriveNetwork
}
| {
provider?: AuthProvider
apiKey?: string
url: string
network?: null
}

export const createAutoDriveApi = ({
provider = 'apikey',
apiKey,
url = 'https://demo.auto-drive.autonomys.xyz/api',
}: {
provider?: AuthProvider
apiKey: string
url?: string
}): AutoDriveApi => {
url = null,
network,
}: ConnectionOptions): AutoDriveApi => {
const baseUrl = !network ? url : getNetworkUrl(network)

return {
sendRequest: async (relativeUrl: string, request: Partial<Request>, body?: BodyInit) => {
const headers = new Headers({
Expand All @@ -36,7 +52,7 @@ export const createAutoDriveApi = ({
body,
}

return fetch(`${url}${relativeUrl}`, fullRequest)
return fetch(`${baseUrl}${relativeUrl}`, fullRequest)
},
}
}
16 changes: 16 additions & 0 deletions packages/auto-drive/src/api/networks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NetworkId } from '@autonomys/auto-utils'

export type AutoDriveNetwork = keyof typeof networks

export const networks = {
[NetworkId.TAURUS]: 'https://demo.auto-drive.autonomys.xyz/api',
[NetworkId.MAINNET]: 'https://mainnet.auto-drive.autonomys.xyz/api',
} satisfies Partial<Record<NetworkId, string>>

export const getNetworkUrl = (networkId: AutoDriveNetwork) => {
if (!networks[networkId]) {
throw new Error(`Network ${networkId} not found`)
}

return networks[networkId]
}
Loading