Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format external url in case it is a direct ipfs link #1181

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading