Skip to content

Commit

Permalink
doc: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
clostao committed Nov 28, 2024
1 parent 557c155 commit 758fa96
Show file tree
Hide file tree
Showing 13 changed files with 1,466 additions and 314 deletions.
2 changes: 2 additions & 0 deletions examples/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AUTO_DRIVE_API_KEY=51c03eb7985c49359eb457a6b7e97b21
AUTO_DRIVE_URL=http://localhost:3000
Binary file added examples/.yarn/install-state.gz
Binary file not shown.
5 changes: 5 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Examples

This folder contains examples in how to use and interact with Auto-Drive backend. Though it's posible to interact directly with the API REST it's recommended to use `@autonomys/auto-drive` npm package.

For using any of these examples, you'd need to have launched auto-drive backend. Read more about this in the root README of this repo.
60 changes: 0 additions & 60 deletions examples/data-operations/index.ts

This file was deleted.

8 changes: 5 additions & 3 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "data-operations-example",
"name": "auto-drive-examples",
"version": "1.0.0",
"main": "index.ts",
"license": "MIT",
"type": "module",
"scripts": {
"data-operations": "ts-node data-operations/index.ts",
"transaction-manager": "ts-node transaction-manager/index.ts"
"simple-cli": "node --loader ts-node/esm simple-cli/index.ts"
},
"dependencies": {
"@autonomys/auto-drive": "^1.0.10",
"axios": "^1.7.5",
"commander": "^12.1.0",
"dotenv": "^16.4.5"
},
"devDependencies": {
Expand Down
36 changes: 36 additions & 0 deletions examples/simple-cli/commands/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { downloadFile } from "@autonomys/auto-drive";
import { Argument, Command } from "commander";
import { api } from "../services/api.js";
import { createWriteStream } from "fs";

export const downloadCommand = new Command("download")
.addArgument(new Argument("cid", "The CID of the file to download"))
.option("-p, --password <password>", "The password to use for the file")
.option("-o, --output <output>", "The path to save the file to")
.action(async (cid, options) => {
const iterable = await downloadFile(api, cid, options.password);
if (options.output) {
await saveIterableInFile(iterable, options.output);
} else {
await logIterable(iterable);
}
});

const saveIterableInFile = async (
iterable: AsyncIterable<Uint8Array>,
output: string
) => {
const writable = createWriteStream(output);
for await (const chunk of iterable) {
writable.write(chunk);
}
writable.close();
};

const logIterable = async (iterable: AsyncIterable<Uint8Array>) => {
let buffer = Buffer.alloc(0);
for await (const chunk of iterable) {
buffer = Buffer.concat([buffer, chunk]);
}
console.log(buffer.toString("utf-8"));
};
26 changes: 26 additions & 0 deletions examples/simple-cli/commands/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Command, Option } from "commander";
import { api } from "../services/api.js";
import { getRoots, Scope } from "@autonomys/auto-drive";

export const listCommand = new Command("ls")
.addOption(
new Option(
"-s, --scope <scope>",
"The scope to list the roots for (user or global)"
)
.choices(["user", "global"])
.default("user")
)
.option("-l, --limit <limit>", "The limit of roots to list", "100")
.option("-o, --offset <offset>", "The offset of roots to list", "0")
.action(async (options) => {
const roots = await getRoots(api, {
scope: options.scope as Scope,
limit: options.limit,
offset: options.offset,
});

roots.rows.forEach((root) => {
console.log(`${root.headCid}: ${root.name}`);
});
});
20 changes: 20 additions & 0 deletions examples/simple-cli/commands/publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Argument, Command } from "commander";
import { uploadFileFromFilepath } from "@autonomys/auto-drive";
import { api } from "../services/api.js";

export const publishCommand = new Command("publish")
.addArgument(new Argument("path", "The path to the file to publish"))
.option("-p, --password <password>", "The password to use for the file")
.option("--no-compression", "Whether to not compress the file", false)
.action(async (path, options) => {
await uploadFileFromFilepath(api, path, {
password: options.password,
compression: options.compression,
})
.promise.then((e) => {
console.log(`Published file with CID: ${e.cid}`);
})
.catch((e) => {
console.error(e);
});
});
13 changes: 13 additions & 0 deletions examples/simple-cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { program } from "commander";
import { publishCommand } from "./commands/publish.js";
import { downloadCommand } from "./commands/download.js";
import { listCommand } from "./commands/list.js";

program
.version("0.0.1")
.description("Auto-Drive CLI")
.addCommand(publishCommand)
.addCommand(downloadCommand)
.addCommand(listCommand);

program.parse(process.argv);
18 changes: 18 additions & 0 deletions examples/simple-cli/services/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createAutoDriveApi } from "@autonomys/auto-drive";
import dotenv from "dotenv";

dotenv.config({
path: ".env.test",
});

const url = process.env.AUTO_DRIVE_URL;
const apiKey = process.env.AUTO_DRIVE_API_KEY;

if (!url || !apiKey) {
throw new Error("AUTO_DRIVE_URL and AUTO_DRIVE_API_KEY must be set");
}

export const api = createAutoDriveApi({
apiKey,
url,
});
54 changes: 0 additions & 54 deletions examples/transaction-manager/index.ts

This file was deleted.

22 changes: 13 additions & 9 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
"compilerOptions": {
"target": "es2018",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist"
},
"include": ["./**/*.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 758fa96

Please sign in to comment.