@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
222 lines • 7.13 kB
JavaScript
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 ?? "",
deletedAt: record.deletedAt ?? null
};
}
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, options) {
const params = new URLSearchParams({ user: owner });
if (cursor !== null) {
params.set("since", cursor);
}
if (options?.includeDeleted) {
params.set("includeDeleted", "true");
}
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}`);
}
},
async deleteFile(params) {
const res = await fetch(`${base}/v1/files/${params.fileId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Web3Signed ${params.signature}`
},
body: JSON.stringify({
ownerAddress: params.ownerAddress
})
});
if (res.status === 409) return;
if (!res.ok) {
throw new Error(`Gateway error: ${res.status} ${res.statusText}`);
}
}
};
}
export {
createGatewayClient
};
//# sourceMappingURL=gateway.js.map