Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
fix(type): simpler api store typings
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbeckers committed Jun 2, 2024
1 parent e88294a commit 74fea7b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hypercerts-org/sdk",
"version": "2.0.0-alpha.9",
"version": "2.0.0-alpha.10",
"description": "SDK for hypercerts protocol",
"repository": "[email protected]:hypercerts-org/hypercerts.git",
"author": "Hypercerts team",
Expand Down
1 change: 1 addition & 0 deletions sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const DEFAULT_INDEXER_ENVIRONMENT: IndexerEnvironment = "all";
// TODO when rolled out to production, enable both testing and prod environments
const ENDPOINTS: { [key: string]: string } = {
metadata: "https://staging-api.hypercerts.org/v1/metadata",
// metadata: "https://hypercerts-api-staging.up.railway.app/v1/metadata",
allowlist: "https://staging-api.hypercerts.org/v1/allowlists",
};

Expand Down
16 changes: 5 additions & 11 deletions sdk/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export class HypercertsStorage implements HypercertStorageInterface {
config,
);

if (!resData) {
throw new StorageError("Failed to store metadata", { errors: {}, cid: undefined });
}

if (!resData.data?.cid || (resData.errors && Object.keys(resData.errors).length > 0)) {
throw new StorageError("Failed to store metadata", { errors: resData.errors, data });
}
Expand Down Expand Up @@ -95,17 +91,15 @@ export class HypercertsStorage implements HypercertStorageInterface {

const resData = await uploadMetadata(metadata, config);

if (!resData) {
throw new StorageError("Failed to store metadata", { errors: {}, cid: undefined });
}

if (!resData.data?.cid || (resData.errors && Object.keys(resData.errors).length > 0)) {
if (!resData?.cid || (resData.errors && Object.keys(resData.errors).length > 0)) {
throw new StorageError("Failed to store metadata", { errors: resData.errors, data });
}

logger.debug(`Stored metadata at ${resData.data?.cid}`);
const { cid } = resData;

return resData.data?.cid;
logger.debug(`Stored metadata at ${cid}`);

return cid;
}

/**
Expand Down
24 changes: 13 additions & 11 deletions sdk/src/utils/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ type AllowListPostRequest = {
totalUnits: string;
};

/**
* Type for the response data from the API.
*/
type ResponseData<T> = {
success: boolean;
message: string;
data?: T;
errors?: Record<string, string | string[]>;
};
// /**
// * Type for the response data from the API.
// */
// type ResponseData<T> = {
// success: boolean;
// message: string;
// data?: T;
// errors?: Record<string, string | string[]>;
// };

/**
* Axios instance configured with the base URL for the hypercert API.
Expand All @@ -32,8 +32,9 @@ const api = axios.create({ headers: { "Content-Type": "application/json" } });
* @param {StorageConfigOverrides} [config] - An optional configuration object.
* @returns The response data from the API.
*/
//TODO fix response typing based on updated API spec
const uploadMetadata = async (metadata: HypercertMetadata, config: StorageConfigOverrides = { timeout: 0 }) => {
const res = await api.post<ResponseData<{ cid: string }>>(ENDPOINTS.metadata, metadata, config);
const res = await api.post(ENDPOINTS.metadata, metadata, config);

if (!res) {
throw new StorageError("Failed to store metadata", { errors: {}, cid: undefined });
Expand All @@ -50,8 +51,9 @@ const uploadMetadata = async (metadata: HypercertMetadata, config: StorageConfig
* @returns The response data from the API.
*
*/
//TODO fix response typing based on updated API spec
const uploadAllowlist = async (req: AllowListPostRequest, config: StorageConfigOverrides = { timeout: 0 }) => {
const res = await api.post<ResponseData<{ cid: string }>>(ENDPOINTS.allowlist, req, config);
const res = await api.post(ENDPOINTS.allowlist, req, config);

if (!res) {
throw new StorageError("Failed to store allow list", { errors: {}, cid: undefined });
Expand Down

0 comments on commit 74fea7b

Please sign in to comment.