Skip to content

Commit

Permalink
add EJS for returning object with undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
DonFungible committed Oct 19, 2024
1 parent 6fcfb9c commit f51e177
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 104 deletions.
2 changes: 1 addition & 1 deletion packages/core-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if (typeof process !== "undefined") {
export class StoryClient {
private readonly config: StoryConfig & { chainId: SupportedChainIds };
private readonly rpcClient: PublicClient;
private readonly wallet: SimpleWalletClient;
public readonly wallet: SimpleWalletClient;
private _ipAsset: IPAssetClient | null = null;
private _permission: PermissionClient | null = null;
private _license: LicenseClient | null = null;
Expand Down
41 changes: 31 additions & 10 deletions packages/react-sdk/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ const visit = (file) => {
if (
ts.isMethodDeclaration(member) &&
(member.modifiers?.some(
(m) => m.kind === ts.SyntaxKind.PublicKeyword
(m) => m.kind === ts.SyntaxKind.PublicKeyword,
) ??
true) &&
member.name &&
ts.isIdentifier(member.name)
) {
const requests = [];
const isAsync = member.modifiers?.some(
(modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword
(modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword,
);
program.getTypeChecker().getSignatureFromDeclaration(member);
member.parameters.forEach((parameter) => {
Expand All @@ -72,7 +72,7 @@ const visit = (file) => {
ts
.getLeadingCommentRanges(sourceFile.text, member.pos)
?.map((range) =>
sourceFile.text.substring(range.pos, range.end).trim()
sourceFile.text.substring(range.pos, range.end).trim(),
) || [],
defaultValues: undefined, // Get default value,
};
Expand Down Expand Up @@ -104,16 +104,16 @@ files.forEach((file, index) => {
(acc, curr) =>
acc.concat(
curr.requests.map((item) => item.type),
curr.responseType
curr.responseType,
),
[]
[],
);
const filteredTypes = [
...new Set(
types
.filter((type) => !isPrimitiveType(type))
.filter((type) => !isViemType(type))
.filter((type) => !isEnclosedInCurlyBraces(type))
.filter((type) => !isEnclosedInCurlyBraces(type)),
),
];
exportTypes.push(...filteredTypes);
Expand All @@ -123,8 +123,29 @@ files.forEach((file, index) => {
name: fileName,
methodNames: asyncMethods,
viemTypes: [...new Set(types.filter((type) => isViemType(type)))],
})
}),
);

const returnObjectWithUndefined = asyncMethods.reduce((acc, method) => {
acc[method] = undefined;
return acc;
}, {});

sources.push(
ejs.render(
`
if (!client) {
return {
loadings,
errors,
<%= Object.entries(returnObjectWithUndefined).map(([key, value]) => \`\${key}: \${value}\`).join(',\\n ') %>
};
}
`,
{ returnObjectWithUndefined },
),
);

const methodTemplates = methods.map((method) => {
return ejs.render(resourceTemplate.methodTemplate, {
method: method,
Expand All @@ -138,7 +159,7 @@ files.forEach((file, index) => {
ejs.render(resourceTemplate.endTemplate, {
methodNames,
name: fileName,
})
}),
);
fs.writeFileSync(`src/resources/use${fileName}.ts`, sources.join("\n"));
});
Expand All @@ -151,7 +172,7 @@ exec("npm run fix", (error) => {
if (error) {
console.log(
"\x1b[31m%s\x1b[0m",
"\nError occurred while running npm run fix command. Please run `npm run fix` manually."
"\nError occurred while running npm run fix command. Please run `npm run fix` manually.",
);
bar.stop();
return;
Expand All @@ -160,6 +181,6 @@ exec("npm run fix", (error) => {
bar.stop();
console.log(
"\x1b[32m%s\x1b[0m",
"React SDK templates generated successfully!"
"React SDK templates generated successfully!",
);
});
10 changes: 9 additions & 1 deletion packages/react-sdk/src/StoryProtocolContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ const StoryContext = createContext<StoryClient>({} as StoryClient);
const StoryProvider = ({ config, children }: Props) => {
const [client, setClient] = useState<StoryClient>();

if (!client) {
if (!config.wallet) {
setClient(
StoryClient.newClient({
transport: config.transport,
}),
);
} else if (!client) {
setClient(StoryClient.newClient(config));
}

return (
<StoryContext.Provider value={client!}>{children}</StoryContext.Provider>
);
};

const useStoryContext = (): StoryClient => {
return useContext(StoryContext);
};
Expand Down
16 changes: 13 additions & 3 deletions packages/react-sdk/src/resources/useDispute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const useDispute = () => {
resolveDispute: null,
});

if (!client) {
return {
loadings,
errors,
raiseDispute: undefined,
cancelDispute: undefined,
resolveDispute: undefined,
};
}

/**
* Raises a dispute on a given ipId
* @param request - The request object containing necessary data to raise a dispute.
Expand All @@ -47,7 +57,7 @@ const useDispute = () => {
"raiseDispute",
client.dispute.raiseDispute.bind(client.dispute),
setLoadings,
setErrors
setErrors,
);

/**
Expand All @@ -70,7 +80,7 @@ const useDispute = () => {
"cancelDispute",
client.dispute.cancelDispute.bind(client.dispute),
setLoadings,
setErrors
setErrors,
);

/**
Expand All @@ -91,7 +101,7 @@ const useDispute = () => {
"resolveDispute",
client.dispute.resolveDispute.bind(client.dispute),
setLoadings,
setErrors
setErrors,
);

return {
Expand Down
19 changes: 15 additions & 4 deletions packages/react-sdk/src/resources/useIpAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ const useIpAccount = () => {
getToken: null,
});

if (!client) {
return {
loadings,
errors,
execute: undefined,
executeWithSig: undefined,
getIpAccountNonce: undefined,
getToken: undefined,
};
}

/** Executes a transaction from the IP Account.
* @param request - The request object containing necessary data to execute IP Account a transaction.
* @param request.ipId The Ip Id to get ip account.
Expand All @@ -44,7 +55,7 @@ const useIpAccount = () => {
"execute",
client.ipAccount.execute.bind(client.ipAccount),
setLoadings,
setErrors
setErrors,
);

/** Executes a transaction from the IP Account.
Expand All @@ -66,7 +77,7 @@ const useIpAccount = () => {
"executeWithSig",
client.ipAccount.executeWithSig.bind(client.ipAccount),
setLoadings,
setErrors
setErrors,
);

/** Returns the IPAccount&#39;s internal nonce for transaction ordering.
Expand All @@ -80,7 +91,7 @@ const useIpAccount = () => {
"getIpAccountNonce",
client.ipAccount.getIpAccountNonce.bind(client.ipAccount),
setLoadings,
setErrors
setErrors,
);

/**
Expand All @@ -91,7 +102,7 @@ const useIpAccount = () => {
"getToken",
client.ipAccount.getToken.bind(client.ipAccount),
setLoadings,
setErrors
setErrors,
);

return {
Expand Down
30 changes: 22 additions & 8 deletions packages/react-sdk/src/resources/useIpAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ const useIpAsset = () => {
mintAndRegisterIpAndMakeDerivative: null,
});

if (!client) {
return {
loadings,
errors,
register: undefined,
registerDerivative: undefined,
registerDerivativeWithLicenseTokens: undefined,
mintAndRegisterIpAssetWithPilTerms: undefined,
registerIpAndAttachPilTerms: undefined,
registerDerivativeIp: undefined,
mintAndRegisterIpAndMakeDerivative: undefined,
};
}

/**
* Create a new `IpCreator` object with the specified details.
* @param params - The parameters required to create the `IpCreator` object.
Expand All @@ -58,7 +72,7 @@ const useIpAsset = () => {
* @returns An `IpCreator` object containing the provided details.
*/
const generateCreatorMetadata = (
param: GenerateCreatorMetadataParam
param: GenerateCreatorMetadataParam,
): IpCreator => {
return client.ipAsset.generateCreatorMetadata(param);
};
Expand Down Expand Up @@ -128,7 +142,7 @@ const useIpAsset = () => {
"register",
client.ipAsset.register.bind(client.ipAsset),
setLoadings,
setErrors
setErrors,
);

/**
Expand All @@ -151,7 +165,7 @@ const useIpAsset = () => {
"registerDerivative",
client.ipAsset.registerDerivative.bind(client.ipAsset),
setLoadings,
setErrors
setErrors,
);

/**
Expand All @@ -172,7 +186,7 @@ const useIpAsset = () => {
"registerDerivativeWithLicenseTokens",
client.ipAsset.registerDerivativeWithLicenseTokens.bind(client.ipAsset),
setLoadings,
setErrors
setErrors,
);

/**
Expand Down Expand Up @@ -201,7 +215,7 @@ const useIpAsset = () => {
"mintAndRegisterIpAssetWithPilTerms",
client.ipAsset.mintAndRegisterIpAssetWithPilTerms.bind(client.ipAsset),
setLoadings,
setErrors
setErrors,
);

/**
Expand Down Expand Up @@ -230,7 +244,7 @@ const useIpAsset = () => {
"registerIpAndAttachPilTerms",
client.ipAsset.registerIpAndAttachPilTerms.bind(client.ipAsset),
setLoadings,
setErrors
setErrors,
);

/**
Expand Down Expand Up @@ -259,7 +273,7 @@ const useIpAsset = () => {
"registerDerivativeIp",
client.ipAsset.registerDerivativeIp.bind(client.ipAsset),
setLoadings,
setErrors
setErrors,
);

/**
Expand Down Expand Up @@ -287,7 +301,7 @@ const useIpAsset = () => {
"mintAndRegisterIpAndMakeDerivative",
client.ipAsset.mintAndRegisterIpAndMakeDerivative.bind(client.ipAsset),
setLoadings,
setErrors
setErrors,
);

return {
Expand Down
Loading

0 comments on commit f51e177

Please sign in to comment.