[Chore] Enhance tsconfig and error handling across multiple packages (#3506)

This commit is contained in:
Jonas Daniels
2024-06-25 21:43:50 -07:00
committed by GitHub
parent 724ef108dc
commit fbb1493f1e
27 changed files with 36 additions and 26 deletions
+1 -1
View File
@@ -56,7 +56,7 @@
"license": "Apache-2.0",
"sideEffects": false,
"scripts": {
"build": "preconstruct build",
"build": "tsc && preconstruct build",
"format": "biome format ./src --write",
"lint": "eslint src/",
"fix": "eslint src/ --fix",
@@ -116,6 +116,9 @@ export async function generateJWT({
export function parseJWT(jwt: string): AuthenticationPayload {
const encodedPayload = jwt.split(".")[1];
const encodedSignature = jwt.split(".")[2];
if(!encodedPayload || !encodedSignature) {
throw new Error(`Invalid JWT token`);
}
const payload: AuthenticationPayloadData = JSON.parse(
base64decode(encodedPayload),
);
+1 -2
View File
@@ -1,5 +1,4 @@
import { ThirdwebAuth } from "../core";
import { LoginPayload, VerifyOptions } from "../core/schema";
import { LoginPayload, ThirdwebAuth, VerifyOptions } from "../core";
import { EthersWallet } from "@thirdweb-dev/wallets/evm/wallets/ethers";
import { ethers } from "ethers";
@@ -23,6 +23,7 @@ export function ThirdwebAuthProvider(cfg: ThirdwebProviderConfig) {
try {
const parsedPayload: LoginPayload = JSON.parse(payload);
const verifyOptions: VerifyOptions = {
domain: cfg.domain,
statement: cfg.authOptions?.statement,
uri: cfg.authOptions?.uri,
version: cfg.authOptions?.version,
@@ -8,7 +8,6 @@ import switchAccountHandler from "./routes/switch-account";
import {
ThirdwebAuthConfig,
ThirdwebAuthContext,
ThirdwebAuthRoute,
} from "./types";
import { NextRequest } from "next/server";
import {
@@ -16,6 +15,7 @@ import {
NextApiRequest,
NextApiResponse,
} from "next/types";
import { ThirdwebAuthRoute } from "../common/types";
export * from "./types";
+1 -1
View File
@@ -1,5 +1,5 @@
{
"extends": "@thirdweb-dev/tsconfig/sdk.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
"exclude": ["dist", "build", "node_modules"]
}
@@ -181,6 +181,7 @@ export function getChainBySlug<TSlug extends ChainSlug>(
const chainIdsBySlug = getChainIdsBySlug();
const chainsById = getChainsById();
return chainsById[
// @ts-expect-error - TODO: should check index access
chainIdsBySlug[slug]
] as ChainsById[ChainIdsBySlug[TSlug]];
}
+1
View File
@@ -10,6 +10,7 @@ const chainCache: Record<number, Chain> = {};
const slugToChainId: Record<string, keyof typeof chainCache> = {};
export async function fetchChain(chainIdOrSlug: number | string) {
if (typeof chainIdOrSlug === "string" && chainIdOrSlug in slugToChainId) {
// @ts-expect-error - TODO: should check index access
return chainCache[slugToChainId[chainIdOrSlug]];
} else if (typeof chainIdOrSlug === "number" && chainIdOrSlug in chainCache) {
return chainCache[chainIdOrSlug];
+1
View File
@@ -8542,6 +8542,7 @@ export function getChainBySlug<TSlug extends ChainSlug>(
const chainIdsBySlug = getChainIdsBySlug();
const chainsById = getChainsById();
return chainsById[
// @ts-expect-error - TODO: should check index access
chainIdsBySlug[slug]
] as ChainsById[ChainIdsBySlug[TSlug]];
}
+2
View File
@@ -147,6 +147,7 @@ export function getChainRPC(
chain: Pick<Chain, "rpc" | "chainId">,
options?: ChainRPCOptions,
): string {
// @ts-expect-error - TODO: should check index access
return getChainRPCs(chain, options)[0];
}
@@ -155,6 +156,7 @@ export function minimizeChain(chain: Chain): MinimalChain {
return {
name: chain.name,
chain: chain.chain,
// @ts-expect-error - TODO: should check index access
rpc: [firstRpc],
nativeCurrency: chain.nativeCurrency,
shortName: chain.shortName,
+1 -4
View File
@@ -1,8 +1,5 @@
{
"extends": "@thirdweb-dev/tsconfig/sdk.json",
"include": ["src", "types"],
"exclude": ["dist", "build", "node_modules"],
"compilerOptions": {
"noUncheckedIndexedAccess": true
}
"exclude": ["dist", "build", "node_modules"]
}
@@ -117,6 +117,7 @@ export const ThirdwebProviderCore = <TChains extends Chain[]>(
const [supportedChainsWithKey, activeChainIdOrObjWithKey] =
useUpdateChainsWithClientId(
supportedChainsNonNull,
// @ts-expect-error - TODO: fix unchecked access
props.activeChain || supportedChainsNonNull[0],
props.clientId,
);
@@ -191,6 +191,7 @@ function useWalletConnectionSetup(
undefined,
);
// @ts-expect-error - TODO: fix unchecked access
const walletParams: WalletOptions = useMemo(() => {
return {
chains: chains,
@@ -66,6 +66,7 @@ const WrappedThirdwebSDKProvider = <TChains extends Chain[]>({
readonlySettings = {
chainId: supportedChain.chainId,
// @ts-expect-error - TODO: fix unchecked access
rpcUrl,
};
} catch (e) {
@@ -100,6 +101,7 @@ const WrappedThirdwebSDKProvider = <TChains extends Chain[]>({
// if we still have no sdk fall back to the first element in chains
if (!sdk_) {
if (supportedChains.length > 0) {
// @ts-expect-error - TODO: fix unchecked access
chainId = supportedChains[0].chainId;
sdk_ = new ThirdwebSDK(chainId, mergedOptions, storageInterface);
} else {
@@ -320,6 +322,7 @@ export const ThirdwebSDKProvider = <TChains extends Chain[]>(
const [supportedChainsWithKey, activeChainIdOrObjWithKey] =
useUpdateChainsWithClientId(
supportedChainsNonNull,
// @ts-expect-error - TODO: fix unchecked access
activeChain || supportedChainsNonNull[0],
clientId,
);
-1
View File
@@ -3,7 +3,6 @@
"include": ["."],
"exclude": ["dist", "build", "node_modules"],
"compilerOptions": {
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"esModuleInterop": true
}
+2 -1
View File
@@ -3,6 +3,7 @@
"include": ["src", "types"],
"exclude": ["dist", "build", "node_modules"],
"compilerOptions": {
"allowJs": true
"allowJs": true,
"noUncheckedIndexedAccess": false // explicitly set to false (default is true)
}
}
@@ -81,6 +81,7 @@ export function getGatewayUrlForCid(
): string {
const parts = cid.split("/");
// @ts-expect-error - TODO: should check index access
const hash = convertCidToV1(parts[0]);
const filePath = parts.slice(1).join("/");
@@ -126,6 +127,7 @@ export function prepareGatewayUrls(
};
for (const key of Object.keys(allGatewayUrls)) {
// @ts-expect-error - TODO: should check index access
const cleanedGatewayUrls = allGatewayUrls[key]
.map((url) => {
// inject clientId when present
+3 -1
View File
@@ -119,7 +119,7 @@ export function replaceGatewayUrlWithScheme(
gatewayUrls: GatewayUrls,
): string {
for (const scheme of Object.keys(gatewayUrls)) {
for (const gatewayUrl of gatewayUrls[scheme]) {
for (const gatewayUrl of (gatewayUrls?.[scheme] || [])) {
// If the url is a tokenized url, we need to convert it to a canonical url
// Otherwise, we just need to check if the url is a prefix of the uri
if (gatewayUrl.includes("{cid}")) {
@@ -153,6 +153,7 @@ export function replaceSchemeWithGatewayUrl(
const scheme = Object.keys(gatewayUrls).find((s) => uri.startsWith(s));
const schemeGatewayUrls = scheme ? gatewayUrls[scheme] : [];
// @ts-expect-error - TODO: should check index access
if ((!scheme && index > 0) || (scheme && index >= schemeGatewayUrls.length)) {
return undefined;
}
@@ -164,6 +165,7 @@ export function replaceSchemeWithGatewayUrl(
const path = uri.replace(scheme, "");
try {
const gatewayUrl = getGatewayUrlForCid(
// @ts-expect-error - TODO: should check index access
schemeGatewayUrls[index],
path,
clientId,
@@ -20,6 +20,7 @@ export class MockDownloader implements IStorageDownloader {
const [cid, name] = url.includes("mock://")
? url.replace("mock://", "").split("/")
: url.replace("ipfs://", "").split("/");
// @ts-expect-error - TODO: should check index access
const data = name ? this.storage[cid][name] : this.storage[cid];
return {
@@ -175,6 +175,7 @@ export class ThirdwebStorage<T extends UploadOptions = IpfsUploadBatchOptions>
*/
async upload(data: unknown, options?: T): Promise<string> {
const [uri] = await this.uploadBatch([data], options);
// @ts-expect-error - TODO: should check index access
return uri;
}
@@ -139,6 +139,7 @@ export class IpfsUploader implements IStorageUploader<IpfsUploadBatchOptions> {
}
// add it to the map so that we can check for duplicates
// @ts-expect-error - TODO: should check index access
fileNameToFileMap.set(fileName, file);
// add it to the filenames array so that we can return the correct number of urls
fileNames.push(fileName);
+1 -1
View File
@@ -1,5 +1,5 @@
{
"extends": "@thirdweb-dev/tsconfig/sdk.json",
"include": ["."],
"include": ["src"],
"exclude": ["dist", "build", "node_modules"]
}
@@ -1,5 +0,0 @@
{
"extends": "./tsconfig.json",
// no idea why this fixes tests but it does?
"files": true
}
+1 -4
View File
@@ -1,7 +1,4 @@
{
"extends": "@thirdweb-dev/tsconfig/sdk.json",
"include": ["${configDir}/src", "${configDir}/test"],
"compilerOptions": {
"noUncheckedIndexedAccess": true
}
"include": ["${configDir}/src", "${configDir}/test"]
}
@@ -140,7 +140,7 @@ export async function extractAuthorizationData(
const authHeader = headers.get("authorization");
if (authHeader) {
const [type, token] = authHeader.split(" ");
if (type.toLowerCase() === "bearer" && !!token) {
if (type?.toLowerCase() === "bearer" && !!token) {
jwt = token;
}
}
+2 -2
View File
@@ -70,7 +70,7 @@ function getHeader(
): string | null {
const header = headers[headerName];
if (Array.isArray(header)) {
return header[0];
return header?.[0] ?? null;
}
return header ?? null;
}
@@ -147,7 +147,7 @@ export function extractAuthorizationData(
const authorizationHeader = getHeader(headers, "authorization");
if (authorizationHeader) {
const [type, token] = authorizationHeader.split(" ");
if (type.toLowerCase() === "bearer" && !!token) {
if (type?.toLowerCase() === "bearer" && !!token) {
jwt = token;
const walletAuthHeader = getHeader(headers, "x-authorize-wallet");
// IK a stringified boolean is not ideal, but it's required to pass it in the headers.
+2 -1
View File
@@ -15,7 +15,8 @@
"preserveWatchOutput": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true
"noEmit": true,
"noUncheckedIndexedAccess": true
},
"exclude": ["node_modules"]
}