sdk.do
Version:
180 lines (179 loc) • 5.24 kB
JavaScript
class ApisAPI {
baseUrl;
apiKey;
headers;
constructor(options = {}) {
this.baseUrl = options.baseUrl || 'https://api.do';
this.apiKey = options.apiKey;
this.headers = {
'Content-Type': 'application/json',
...options.headers,
};
if (this.apiKey) {
this.headers['Authorization'] = `Bearer ${this.apiKey}`;
}
}
async get(path, params) {
const url = new URL(path, this.baseUrl);
if (params) {
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined) {
url.searchParams.append(key, String(value));
}
});
}
const response = await fetch(url.toString(), {
method: 'GET',
headers: this.headers,
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || 'API request failed');
}
return response.json();
}
async post(path, data) {
const url = new URL(path, this.baseUrl);
const response = await fetch(url.toString(), {
method: 'POST',
headers: this.headers,
body: data ? JSON.stringify(data) : undefined,
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || 'API request failed');
}
return response.json();
}
async put(path, data) {
const url = new URL(path, this.baseUrl);
const response = await fetch(url.toString(), {
method: 'PUT',
headers: this.headers,
body: data ? JSON.stringify(data) : undefined,
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || 'API request failed');
}
return response.json();
}
async patch(path, data) {
const url = new URL(path, this.baseUrl);
const response = await fetch(url.toString(), {
method: 'PATCH',
headers: this.headers,
body: data ? JSON.stringify(data) : undefined,
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || 'API request failed');
}
return response.json();
}
async delete(path) {
const url = new URL(path, this.baseUrl);
const response = await fetch(url.toString(), {
method: 'DELETE',
headers: this.headers,
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || 'API request failed');
}
return response.json();
}
async list(resource, params) {
return this.get(`/v1/${resource}`, params);
}
async getById(resource, id) {
return this.get(`/v1/${resource}/${id}`);
}
async create(resource, data) {
return this.post(`/v1/${resource}`, data);
}
async update(resource, id, data) {
return this.put(`/v1/${resource}/${id}`, data);
}
async remove(resource, id) {
return this.delete(`/v1/${resource}/${id}`);
}
}
export class API {
api;
constructor(options = {}) {
this.api = new ApisAPI(options);
}
/**
* List all packages
*/
async listPackages(params) {
return this.api.list('packages', params);
}
/**
* Get a package by ID
*/
async getPackage(id) {
return this.api.getById('packages', id);
}
/**
* Create a new package
*/
async createPackage(data) {
return this.api.create('packages', data);
}
/**
* Update a package
*/
async updatePackage(id, data) {
return this.api.update('packages', id, data);
}
/**
* Delete a package
*/
async deletePackage(id) {
return this.api.remove('packages', id);
}
/**
* Publish a package to NPM
*/
async publishPackage(id, options = {}) {
return this.api.post(`/v1/packages/${id}/publish`, options);
}
/**
* List all functions
*/
async listFunctions(params) {
return this.api.list('functions', params);
}
/**
* List all workflows
*/
async listWorkflows(params) {
return this.api.list('workflows', params);
}
/**
* List all databases
*/
async listDatabases(params) {
return this.api.list('databases', params);
}
/**
* Add a collection to a package
*/
async addCollectionToPackage(packageId, collection) {
return this.api.post(`/v1/packages/${packageId}/collections`, { collection });
}
/**
* Remove a collection from a package
*/
async removeCollectionFromPackage(packageId, collection) {
return this.api.delete(`/v1/packages/${packageId}/collections/${collection}`);
}
/**
* Update package.json for a package
*/
async updatePackageJson(packageId, packageJson) {
return this.api.patch(`/v1/packages/${packageId}`, { package: packageJson });
}
}