Skip to content

Commit

Permalink
format external url in case it is a direct ipfs link (#1181)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jipperism authored Nov 13, 2023
1 parent b3b237c commit e622aba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
17 changes: 17 additions & 0 deletions frontend/lib/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from "lodash";
import { HypercertClient } from "@hypercerts-org/sdk";

export const formatScope = (scopeLabel: string) =>
scopeLabel.toLowerCase().replaceAll(/\s+/g, "-").trim();
Expand Down Expand Up @@ -72,3 +73,19 @@ export const formatAddress = (address: string) =>
*/
export const cidToIpfsUri = (cid: string) =>
cid.startsWith("ipfs://") ? cid : `ipfs://${cid}`;

export const formatExternalUrl = (
client: HypercertClient,
externalUrl?: string,
) => {
if (!externalUrl) {
return "";
}
if (!externalUrl.startsWith("ipfs://")) {
return externalUrl;
}

return client.storage.getNftStorageGatewayUri(
externalUrl.replace("ipfs://", ""),
);
};
28 changes: 22 additions & 6 deletions frontend/lib/hypercert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
HypercertClient,
} from "@hypercerts-org/sdk";
import BN from "bn.js";
import { formatExternalUrl } from "./formatting";

export interface Hypercert {
getTokensFor(owner: string): HypercertTokens;
metadata?: HypercertMetadata;
metadata?: HypercertMetadata & { external_uri_formatted?: string };
claim?: ClaimByIdQuery["claim"];
claimTokens?: ClaimTokensByClaimQuery["claimTokens"];
name: string;
Expand Down Expand Up @@ -39,13 +40,25 @@ export async function loadHypercert(
: claimQueryResp.claim?.uri;
if (metadataUri) {
const metadata = await client.storage.getMetadata(metadataUri);
hypercert.metadata = metadata;
hypercert.metadata = {
...metadata,
external_url_formatted: formatExternalUrl(
client,
metadata.external_url,
),
};
}
return hypercert;
} else {
if (options.metadataUri) {
const metadata = await client.storage.getMetadata(options.metadataUri);
return new MetadataOnlyHypercert(options.metadataUri, metadata);
return new MetadataOnlyHypercert(options.metadataUri, {
...metadata,
external_url_formatted: formatExternalUrl(
client,
metadata.external_url,
),
});
}
throw new Error(
"A metadataUri or claimId are required to load a hypercert",
Expand Down Expand Up @@ -102,10 +115,13 @@ export class HypercertTokens {
export class MetadataOnlyHypercert implements Hypercert {
claim?: ClaimByIdQuery["claim"];
claimTokens?: ClaimTokensByClaimQuery["claimTokens"];
metadata: HypercertMetadata;
metadata: HypercertMetadata & { external_url_formatted?: string };
metadataUri: string;

constructor(metadataUri: string, metadata: HypercertMetadata) {
constructor(
metadataUri: string,
metadata: HypercertMetadata & { external_url_formatted?: string },
) {
this.metadata = metadata;
this.metadataUri = metadataUri;
}
Expand Down Expand Up @@ -139,7 +155,7 @@ export class FullHypercert implements Hypercert {
// previous HypercertData type
claim: ClaimByIdQuery["claim"];
claimTokens: ClaimTokensByClaimQuery["claimTokens"];
metadata?: HypercertMetadata;
metadata?: HypercertMetadata & { external_url_formatted?: string };

constructor(
claimQueryResp: ClaimByIdQuery,
Expand Down

0 comments on commit e622aba

Please sign in to comment.