import fetch from "cross-fetch"; import { GetNftResponse, SendTransactionParams, SendTransactionResponse, SmartContract, } from "./types"; import { RippinPlugin } from "./types/plugin"; import { Blockchain, MasterNft, Project, ProjectUser } from "./types/projects"; import objectToQueryString from "./utils/objectToQueryString"; export class RippinClient { baseUrl: string; projectId: string; constructor({ environment, projectId, }: { environment: "dev" | "sandbox" | "production"; projectId: string; }) { this.baseUrl = environment === "dev" ? "http://localhost:3000" : environment === "sandbox" ? "https://staging-api.rippin.io" : "https://api.rippin.io"; this.projectId = projectId; } /** * Initialize a Rippin plugin. * @param plugin * @returns */ use = (plugin: RippinPlugin) => { plugin.init(this); return this; }; /** @internal */ _createHeaders = async () => { if (this.auth) { return this.auth().createHeaders(); } return {}; }; /** @internal */ _fetch = async ( path: string, { body, headers, ...options }: Omit & { body?: Record } = {} ) => { const resp = await fetch(`${this.baseUrl}/${path}`, { headers: { "Content-Type": "application/json", ...(await this._createHeaders()), ...headers, }, mode: "cors", ...options, body: body ? JSON.stringify(body) : undefined, }); if (resp.status >= 400) { const json = await resp.json(); throw Error(JSON.stringify(json)); } return resp; }; /** @internal */ _fetchFormData = async ( path: string, { body, headers, ...options }: Omit & { body?: FormData } = {} ) => { const resp = await fetch(`${this.baseUrl}/${path}`, { headers: { ...(await this._createHeaders()), ...headers, }, mode: "cors", ...options, body, }); if (resp.status >= 400) { const json = await resp.json(); throw Error(JSON.stringify(json)); } return resp; }; /** * Get Project with id _id_ */ getProject = async ({ id }: { id: string }) => { const resp = await this._fetch(`projects/${id}`); const json = await resp.json(); return json as Project; }; /** * Create a "user" wallet associated with the project used to instantiated the SDK. * * @param externalId - [recommended] parameter to add an external association within Rippin. * This allows the developer to interact with wallets using their preferred ID scheme. */ createUserWallet = async (externalId?: string) => { const resp = await this._fetch(`projects/${this.projectId}/users`, { method: "POST", body: { project_id: this.projectId, external_id: externalId, }, }); const json = await resp.json(); return json as ProjectUser; }; /** * Get a "user" wallet by external ID. * * @param externalId */ getUserWalletByExternalId = async (externalId: string) => { const resp = await this._fetch( `projects/${this.projectId}/users/${externalId}` ); const json = await resp.json(); return json as ProjectUser; }; /** * Revoke Project ApiKey with id _id_ */ revokeProjectApiKey = async ({ id, projectId, }: { id: string; projectId: string; }) => { const resp = await this._fetch(`project_api_keys/${id}`, { method: "DELETE", body: { project_api_key: { projectId, }, }, }); const json = await resp.json(); return json as { success: boolean }; }; getProjectNfts = async ({ projectId }: { projectId: string }) => { const resp = await this._fetch(`master_nfts?project_id=${projectId}`); const json = await resp.json(); return json.data as MasterNft[]; }; createProjectNft = async ({ data, media, projectId, name, maxSupply, smartContractId, }: { data?: Record; media?: File; projectId: string; name: string; maxSupply?: string | number; smartContractId?: string; }) => { const formData = new FormData(); formData.append("master_nft[project_id]", projectId); formData.append("master_nft[name]", name); if (data && Object.keys(data).length) { Object.keys(data).forEach((key) => { formData.append(`master_nft[data][${key}]`, data[key]); }); } if (smartContractId) { formData.append("master_nft[smart_contract_id]", smartContractId); } if (media) { formData.append("master_nft[media]", media); } if (maxSupply) { formData.append("master_nft[max_supply]", maxSupply.toString()); } const resp = await this._fetchFormData("master_nfts", { method: "POST", body: formData, }); const json = await resp.json(); return json as MasterNft; }; getNft = async ({ id }: { id: string }) => { const resp = await this._fetch(`master_nfts/${id}`); const json = await resp.json(); return json as GetNftResponse; }; updateNft = async ({ data, id, media, name, maxSupply, }: { data?: Record; id: string; media?: File; name?: string; maxSupply?: string | number; }) => { if (!media && !name && !maxSupply) { throw Error("Must update at least 1 field"); } const formData = new FormData(); if (data && Object.keys(data).length) { Object.keys(data).forEach((key) => { formData.append(`master_nft[data][${key}]`, data[key]); }); } if (name) { formData.append("master_nft[name]", name); } if (media) { formData.append("master_nft[media]", media); } if (maxSupply) { formData.append("master_nft[max_supply]", maxSupply.toString()); } const resp = await this._fetchFormData(`master_nfts/${id}`, { method: "PATCH", body: formData, }); const json = await resp.json(); return json as MasterNft; }; updateNftMedia = async ({ id, body }: { id: string; body: FormData }) => { const resp = await this._fetchFormData(`master_nfts/${id}/media`, { method: "PUT", body, }); const json = await resp.json(); return json as {}; }; updateNftMetadata = async ({ id, body }: { id: string; body: FormData }) => { const resp = await this._fetchFormData(`master_nfts/${id}/metadata`, { method: "PUT", body, }); const json = await resp.json(); return json as {}; }; getSmartContractsByProject = async ({ projectId }: { projectId: string }) => { const resp = await this._fetch(`smart_contracts?project_id=${projectId}`); const json = await resp.json(); return json as { data: SmartContract[] }; }; /** * For Solana, tx is a Base64 encoded serialized transaction * @returns */ sendTransaction = async (body: SendTransactionParams) => { const resp = await this._fetch("blockchains/send_transaction", { method: "POST", body, }); const json = await resp.json(); return json as SendTransactionResponse; }; /** * Proxy RPC requests for the selected blockchain. Currently only supports Polygon. * * Note: this is not meant for large scale usage. */ proxyRpc = async (body: { blockchain: Blockchain; method: string; params: any[]; }) => { const resp = await this._fetch("blockchains/proxy_rpc", { method: "POST", body, }); const json = await resp.json(); return json as any; }; }