@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
226 lines • 7.52 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var gateway_exports = {};
__export(gateway_exports, {
createGatewayClient: () => createGatewayClient
});
module.exports = __toCommonJS(gateway_exports);
function createGatewayClient(baseUrl) {
const base = baseUrl.replace(/\/+$/, "");
async function unwrapEnvelope(res) {
const envelope = await res.json();
return envelope.data;
}
function normalizeFileRecord(record) {
return {
fileId: record.fileId ?? record.id ?? "",
owner: record.owner ?? record.ownerAddress ?? "",
url: record.url,
schemaId: record.schemaId,
createdAt: record.createdAt ?? record.addedAt ?? ""
};
}
function getMutationId(body, key) {
const value = body[key] ?? body["id"];
return typeof value === "string" ? value : void 0;
}
return {
async isRegisteredBuilder(address) {
const builder = await this.getBuilder(address);
return builder !== null;
},
async getBuilder(address) {
const res = await fetch(`${base}/v1/builders/${address}`);
if (res.status === 404) return null;
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
return unwrapEnvelope(res);
},
async getGrant(grantId) {
const res = await fetch(`${base}/v1/grants/${grantId}`);
if (res.status === 404) return null;
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
return unwrapEnvelope(res);
},
async listGrantsByUser(userAddress) {
const res = await fetch(`${base}/v1/grants?user=${userAddress}`);
if (res.status === 404) return [];
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
return unwrapEnvelope(res);
},
async getSchemaForScope(scope) {
const res = await fetch(`${base}/v1/schemas?scope=${scope}`);
if (res.status === 404) return null;
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
return unwrapEnvelope(res);
},
async getServer(address) {
const res = await fetch(`${base}/v1/servers/${address}`);
if (res.status === 404) return null;
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
return unwrapEnvelope(res);
},
async getFile(fileId) {
const res = await fetch(`${base}/v1/files/${fileId}`);
if (res.status === 404) return null;
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
return normalizeFileRecord(await unwrapEnvelope(res));
},
async listFilesSince(owner, cursor) {
const params = new URLSearchParams({ user: owner });
if (cursor !== null) {
params.set("since", cursor);
}
const res = await fetch(`${base}/v1/files?${params.toString()}`);
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
const data = await unwrapEnvelope(res);
return {
files: data.files.map(normalizeFileRecord),
cursor: data.cursor
};
},
async getSchema(schemaId) {
const res = await fetch(`${base}/v1/schemas/${schemaId}`);
if (res.status === 404) return null;
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
return unwrapEnvelope(res);
},
async registerServer(params) {
const res = await fetch(`${base}/v1/servers`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Web3Signed ${params.signature}`
},
body: JSON.stringify({
ownerAddress: params.ownerAddress,
serverAddress: params.serverAddress,
publicKey: params.publicKey,
serverUrl: params.serverUrl
})
});
if (res.status === 409) {
const body2 = await res.json().catch(() => ({}));
return {
serverId: getMutationId(body2, "serverId"),
alreadyRegistered: true
};
}
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
const body = await res.json().catch(() => ({}));
return {
serverId: getMutationId(body, "serverId"),
alreadyRegistered: false
};
},
async registerFile(params) {
const res = await fetch(`${base}/v1/files`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Web3Signed ${params.signature}`
},
body: JSON.stringify({
ownerAddress: params.ownerAddress,
url: params.url,
schemaId: params.schemaId
})
});
if (res.status === 409) {
const body2 = await res.json().catch(() => ({}));
return {
fileId: getMutationId(body2, "fileId")
};
}
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
const body = await res.json();
return {
fileId: getMutationId(body, "fileId")
};
},
async createGrant(params) {
const res = await fetch(`${base}/v1/grants`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Web3Signed ${params.signature}`
},
body: JSON.stringify({
grantorAddress: params.grantorAddress,
granteeId: params.granteeId,
grant: params.grant,
fileIds: params.fileIds
})
});
if (res.status === 409) {
const body2 = await res.json().catch(() => ({}));
return {
grantId: getMutationId(body2, "grantId")
};
}
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
const body = await res.json();
return {
grantId: getMutationId(body, "grantId")
};
},
async revokeGrant(params) {
const res = await fetch(`${base}/v1/grants/${params.grantId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Web3Signed ${params.signature}`
},
body: JSON.stringify({
grantorAddress: params.grantorAddress
})
});
if (res.status === 409) return;
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
}
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createGatewayClient
});
//# sourceMappingURL=gateway.cjs.map