Skip to content
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
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,31 @@ const api = new YepCodeApi({ apiToken: '****' });
const processes = await api.getProcesses();
```

### 6. Storage Objects

You can manage files in your YepCode workspace using the `YepCodeStorage` class. This allows you to upload, list, download, and delete files easily.

```js
const { YepCodeStorage } = require('@yepcode/run');
const fs = require('fs');

const storage = new YepCodeStorage({ apiToken: '****' });

// Upload a file (using Node.js stream)
await storage.upload('path/myfile.txt', fs.createReadStream('./myfile.txt'));

// List files
const files = await storage.list();
console.log(files);

// Download a file
const stream = await storage.download('path/myfile.txt');
stream.pipe(fs.createWriteStream('./downloaded.txt'));

// Delete a file
await storage.delete('myfile.txt');
```

## SDK API Reference

### YepCodeRun
Expand Down Expand Up @@ -291,6 +316,55 @@ interface Process {
}
```

### YepCodeStorage

Manages file storage in your YepCode workspace. Allows you to upload, list, download, and delete files using the YepCode API.

#### Constructor

```typescript
constructor(options?: {
apiToken?: string; // Optional if YEPCODE_API_TOKEN env var is set
})
```

#### Methods

##### `upload(filename: string, file: File | Blob | Readable): Promise<StorageObject>`
Uploads a file to YepCode storage.

- `filename`: Name to assign to the uploaded file
- `file`: The file to upload (can be a File, Blob, or Node.js Readable stream)
- **Returns:** Promise<StorageObject>

##### `list(): Promise<StorageObject[]>`
Lists all files in YepCode storage.

- **Returns:** Promise<StorageObject[]>

##### `download(filename: string): Promise<Readable>`
Downloads a file from YepCode storage as a Node.js Readable stream.

- `filename`: Name of the file to download
- **Returns:** Promise<Readable>

##### `delete(filename: string): Promise<void>`
Deletes a file from YepCode storage.

- `filename`: Name of the file to delete
- **Returns:** Promise<void>

##### `StorageObject`
```typescript
interface StorageObject {
name: string;
url: string;
size?: number;
contentType?: string;
createdAt?: string;
}
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
20 changes: 20 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Readable } from "stream";

export interface YepCodeApiConfig {
authUrl?: string;
apiHost?: string;
Expand Down Expand Up @@ -380,3 +382,21 @@ export interface VersionedModuleAliasesPaginatedResult {
total?: number;
data?: VersionedModuleAlias[];
}

/**
* Storage
*/
export type StorageObject = {
name: string;
size: number;
md5Hash: string;
contentType: string;
createdAt: string;
updatedAt: string;
link: URL;
};

export type CreateStorageObjectInput = {
name: string;
file: File | Blob | Readable;
};
93 changes: 86 additions & 7 deletions src/api/yepcodeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import {
VersionedModuleAlias,
VersionedModuleAliasInput,
VersionedModuleAliasesPaginatedResult,
StorageObject,
CreateStorageObjectInput,
} from "./types";
import { Readable } from "stream";

export class YepCodeApiError extends Error {
constructor(message: string, public status: number) {
Expand All @@ -39,6 +42,14 @@ export class YepCodeApiError extends Error {
}
}

type RequestOptions = {
headers?: Record<string, string>;
data?: any;
params?: Record<string, any>;
duplex?: "half" | "full";
responseType?: "stream";
};

export class YepCodeApi {
private apiHost: string;
private clientId?: string;
Expand Down Expand Up @@ -197,28 +208,33 @@ export class YepCodeApi {
private async request<T>(
method: string,
endpoint: string,
options: {
headers?: Record<string, string>;
data?: any;
params?: Record<string, any>;
} = {}
options: RequestOptions = {}
): Promise<T> {
if (!this.accessToken) {
await this.getAccessToken();
}

const isFormData =
typeof FormData !== "undefined" && options.data instanceof FormData;
const isStream =
typeof Readable !== "undefined" && options.data instanceof Readable;

const fetchOptions: RequestInit = {
method,
headers: {
Authorization: `Bearer ${this.accessToken}`,
"Content-Type": "application/json",
...(isFormData || isStream || !options.data
? {}
: { "Content-Type": "application/json" }),
...(options.headers || {}),
},
signal: AbortSignal.timeout(this.timeout),
...(options.duplex && { duplex: options.duplex }),
};

if (options.data) {
fetchOptions.body = JSON.stringify(options.data);
fetchOptions.body =
isFormData || isStream ? options.data : JSON.stringify(options.data);
}

const url = new URL(`${this.getBaseURL()}${endpoint}`);
Expand Down Expand Up @@ -251,6 +267,15 @@ export class YepCodeApi {
);
}

if (options.responseType === "stream") {
if (typeof response.body?.getReader === "function") {
// @ts-ignore
return Readable.fromWeb(response.body) as T;
}
// @ts-ignore
return response.body as T;
}

const responseText = await response.text();
try {
return JSON.parse(responseText);
Expand Down Expand Up @@ -548,4 +573,58 @@ export class YepCodeApi {
): Promise<VersionedModuleAlias> {
return this.request("POST", `/modules/${moduleId}/aliases`, { data });
}

async getObjects(): Promise<StorageObject[]> {
return this.request("GET", "/storage/objects");
}

async getObject(name: string): Promise<Readable> {
return this.request("GET", `/storage/objects/${name}`, {
responseType: "stream",
});
}

async createObject(data: CreateStorageObjectInput): Promise<StorageObject> {
const file = data.file;
if (!file) {
throw new Error("File or stream is required");
}

const isReadable =
typeof Readable !== "undefined" && file instanceof Readable;
const isFile = typeof File !== "undefined" && file instanceof File;
const isBlob = typeof Blob !== "undefined" && file instanceof Blob;
if (!isReadable && !isFile && !isBlob) {
throw new Error(
"Unsupported file type. Must be File, Blob or Readable stream."
);
}

const options: RequestOptions = {};
if (isFile || isBlob) {
const formData = new FormData();
formData.append("file", file);
options.data = formData;
}
if (isReadable) {
options.data = file;
options.duplex = "half";
options.headers = {
"Content-Type": "application/octet-stream",
};
}

return this.request(
"POST",
`/storage/objects?name=${encodeURIComponent(data.name)}`,
options
);
}

async deleteObject(name: string): Promise<void> {
return this.request(
"DELETE",
`/storage/objects/${encodeURIComponent(name)}`
);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { YepCodeRun, Execution } from "./run";
export { YepCodeEnv } from "./env";
export * from "./api";
export * from "./storage";
export * from "./types";
1 change: 1 addition & 0 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./yepcodeStorage";
30 changes: 30 additions & 0 deletions src/storage/yepcodeStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { YepCodeApi, YepCodeApiManager } from "../api";
import { StorageObject, YepCodeApiConfig } from "../api/types";
import { Readable } from "stream";

export class YepCodeStorage {
private api: YepCodeApi;

constructor(config: YepCodeApiConfig = {}) {
this.api = YepCodeApiManager.getInstance(config);
}

async upload(
filename: string,
file: File | Blob | Readable
): Promise<StorageObject> {
return this.api.createObject({ name: filename, file });
}

async list(): Promise<StorageObject[]> {
return this.api.getObjects();
}

async download(filename: string): Promise<Readable> {
return this.api.getObject(filename);
}

async delete(filename: string): Promise<void> {
await this.api.deleteObject(filename);
}
}
1 change: 1 addition & 0 deletions tests/api/test-run-sdk.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, YepCode! This is a test.
Loading