thirdweb 5.57.0-nightly-cf1683c3feea6eb36ac6b902a1dd64c8ed8803c9-20240918000340 → 5.57.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/cjs/client/client.js +19 -15
  2. package/dist/cjs/client/client.js.map +1 -1
  3. package/dist/cjs/extensions/erc1155/drops/write/updateMetadata.js +2 -2
  4. package/dist/cjs/extensions/erc1155/drops/write/updateMetadata.js.map +1 -1
  5. package/dist/cjs/extensions/erc1155/read/getNFTs.js +4 -1
  6. package/dist/cjs/extensions/erc1155/read/getNFTs.js.map +1 -1
  7. package/dist/cjs/extensions/erc721/drops/write/updateMetadata.js +2 -2
  8. package/dist/cjs/extensions/erc721/drops/write/updateMetadata.js.map +1 -1
  9. package/dist/cjs/utils/any-evm/deploy-metadata.js +1 -0
  10. package/dist/cjs/utils/any-evm/deploy-metadata.js.map +1 -1
  11. package/dist/cjs/utils/fetch.js +11 -23
  12. package/dist/cjs/utils/fetch.js.map +1 -1
  13. package/dist/cjs/utils/jwt/is-jwt.js +7 -0
  14. package/dist/cjs/utils/jwt/is-jwt.js.map +1 -0
  15. package/dist/cjs/version.js +1 -1
  16. package/dist/cjs/version.js.map +1 -1
  17. package/dist/esm/client/client.js +19 -15
  18. package/dist/esm/client/client.js.map +1 -1
  19. package/dist/esm/extensions/erc1155/drops/write/updateMetadata.js +2 -2
  20. package/dist/esm/extensions/erc1155/drops/write/updateMetadata.js.map +1 -1
  21. package/dist/esm/extensions/erc1155/read/getNFTs.js +4 -1
  22. package/dist/esm/extensions/erc1155/read/getNFTs.js.map +1 -1
  23. package/dist/esm/extensions/erc721/drops/write/updateMetadata.js +2 -2
  24. package/dist/esm/extensions/erc721/drops/write/updateMetadata.js.map +1 -1
  25. package/dist/esm/utils/any-evm/deploy-metadata.js +1 -0
  26. package/dist/esm/utils/any-evm/deploy-metadata.js.map +1 -1
  27. package/dist/esm/utils/fetch.js +5 -18
  28. package/dist/esm/utils/fetch.js.map +1 -1
  29. package/dist/esm/utils/jwt/is-jwt.js +4 -0
  30. package/dist/esm/utils/jwt/is-jwt.js.map +1 -0
  31. package/dist/esm/version.js +1 -1
  32. package/dist/esm/version.js.map +1 -1
  33. package/dist/types/client/client.d.ts +2 -2
  34. package/dist/types/client/client.d.ts.map +1 -1
  35. package/dist/types/extensions/erc1155/drops/write/updateMetadata.d.ts +1 -3
  36. package/dist/types/extensions/erc1155/drops/write/updateMetadata.d.ts.map +1 -1
  37. package/dist/types/extensions/erc1155/read/getNFTs.d.ts.map +1 -1
  38. package/dist/types/extensions/erc721/drops/write/updateMetadata.d.ts +0 -2
  39. package/dist/types/extensions/erc721/drops/write/updateMetadata.d.ts.map +1 -1
  40. package/dist/types/utils/any-evm/deploy-metadata.d.ts.map +1 -1
  41. package/dist/types/utils/fetch.d.ts +4 -2
  42. package/dist/types/utils/fetch.d.ts.map +1 -1
  43. package/dist/types/utils/jwt/is-jwt.d.ts +3 -0
  44. package/dist/types/utils/jwt/is-jwt.d.ts.map +1 -0
  45. package/dist/types/utils/jwt/types.d.ts +1 -0
  46. package/dist/types/utils/jwt/types.d.ts.map +1 -1
  47. package/dist/types/version.d.ts +1 -1
  48. package/dist/types/version.d.ts.map +1 -1
  49. package/package.json +11 -11
  50. package/src/client/client.test.ts +17 -2
  51. package/src/client/client.ts +23 -17
  52. package/src/extensions/erc1155/drops/write/updateMetadata.test.ts +0 -1
  53. package/src/extensions/erc1155/drops/write/updateMetadata.ts +4 -7
  54. package/src/extensions/erc1155/read/getNFTs.ts +4 -1
  55. package/src/extensions/erc721/drops/write/updateMetadata.test.ts +0 -2
  56. package/src/extensions/erc721/drops/write/updateMetadata.ts +2 -4
  57. package/src/extensions/marketplace/direct-listings/direct-listings.test.ts +317 -394
  58. package/src/utils/any-evm/deploy-metadata.ts +1 -0
  59. package/src/utils/fetch.test.ts +114 -0
  60. package/src/utils/fetch.ts +7 -26
  61. package/src/utils/jwt/is-jwt.ts +5 -0
  62. package/src/utils/jwt/types.ts +2 -0
  63. package/src/version.ts +1 -1
@@ -50,6 +50,7 @@ export async function fetchDeployMetadata(
50
50
  ...parsedMeta,
51
51
  version: rawMeta.version,
52
52
  bytecode: deployBytecode,
53
+ name: rawMeta.name,
53
54
  };
54
55
  }
55
56
 
@@ -0,0 +1,114 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import type { ThirdwebClient } from "../client/client.js";
3
+ import type { Ecosystem } from "../wallets/in-app/web/types.js";
4
+ import {
5
+ IS_THIRDWEB_URL_CACHE,
6
+ getClientFetch,
7
+ isThirdwebUrl,
8
+ } from "./fetch.js";
9
+
10
+ // Mock fetch
11
+ global.fetch = vi.fn();
12
+
13
+ describe("getClientFetch", () => {
14
+ const mockClient: ThirdwebClient = {
15
+ clientId: "test-client-id",
16
+ secretKey: undefined,
17
+ };
18
+ const mockEcosystem: Ecosystem = { id: "ecosystem.test" };
19
+
20
+ beforeEach(() => {
21
+ vi.resetAllMocks();
22
+ });
23
+
24
+ it("should set correct headers for thirdweb URLs", async () => {
25
+ vi.spyOn(global, "fetch").mockResolvedValue(new Response());
26
+ const clientFetch = getClientFetch(mockClient, mockEcosystem);
27
+ await clientFetch("https://api.thirdweb.com/test");
28
+
29
+ expect(global.fetch).toHaveBeenCalledWith(
30
+ "https://api.thirdweb.com/test",
31
+ expect.objectContaining({
32
+ headers: expect.any(Headers),
33
+ }),
34
+ );
35
+
36
+ // biome-ignore lint/suspicious/noExplicitAny: `any` type ok for tests
37
+ const headers = (global.fetch as any).mock.calls[0][1].headers;
38
+ expect(headers.get("x-client-id")).toBe("test-client-id");
39
+ expect(headers.get("x-ecosystem-id")).toBe("ecosystem.test");
40
+ });
41
+
42
+ it("should not set headers for non-thirdweb URLs", async () => {
43
+ vi.spyOn(global, "fetch").mockResolvedValue(new Response());
44
+ const clientFetch = getClientFetch(mockClient, mockEcosystem);
45
+ await clientFetch("https://example.com");
46
+
47
+ expect(global.fetch).toHaveBeenCalledWith(
48
+ "https://example.com",
49
+ expect.not.objectContaining({
50
+ headers: expect.any(Headers),
51
+ }),
52
+ );
53
+ });
54
+
55
+ it("should send a bearer token if secret key is a JWT", () => {
56
+ vi.spyOn(global, "fetch").mockResolvedValue(new Response());
57
+ const clientFetch = getClientFetch({
58
+ clientId: "test-client-id",
59
+ secretKey: "foo.bar.baz",
60
+ });
61
+ clientFetch("https://api.thirdweb.com/test");
62
+
63
+ expect(global.fetch).toHaveBeenCalledWith(
64
+ "https://api.thirdweb.com/test",
65
+ expect.objectContaining({
66
+ headers: expect.any(Headers),
67
+ }),
68
+ );
69
+
70
+ // biome-ignore lint/suspicious/noExplicitAny: `any` type ok for tests
71
+ const headers = (global.fetch as any).mock.calls[0][1].headers;
72
+ expect(headers.get("authorization")).toBe("Bearer foo.bar.baz");
73
+ });
74
+
75
+ it("should abort the request after timeout", async () => {
76
+ vi.useFakeTimers();
77
+ const abortSpy = vi.spyOn(AbortController.prototype, "abort");
78
+ const clientFetch = getClientFetch(mockClient);
79
+
80
+ const fetchPromise = clientFetch("https://api.thirdweb.com/test", {
81
+ requestTimeoutMs: 5000,
82
+ });
83
+ vi.advanceTimersByTime(5001);
84
+
85
+ await expect(fetchPromise).rejects.toThrow();
86
+ expect(abortSpy).toHaveBeenCalled();
87
+
88
+ vi.useRealTimers();
89
+ });
90
+ });
91
+
92
+ describe("isThirdwebUrl", () => {
93
+ it("should return true for thirdweb domains", () => {
94
+ expect(isThirdwebUrl("https://api.thirdweb.com")).toBe(true);
95
+ expect(isThirdwebUrl("https://example.ipfscdn.io")).toBe(true);
96
+ });
97
+
98
+ it("should return false for non-thirdweb domains", () => {
99
+ expect(isThirdwebUrl("https://example.com")).toBe(false);
100
+ expect(isThirdwebUrl("https://otherthirdweb.com")).toBe(false);
101
+ });
102
+
103
+ it("should handle invalid URLs", () => {
104
+ expect(isThirdwebUrl("not-a-url")).toBe(false);
105
+ });
106
+
107
+ it("should cache results", () => {
108
+ const url = "https://api.thirdweb.com";
109
+ isThirdwebUrl(url);
110
+ isThirdwebUrl(url);
111
+ // You might need to expose the cache to test this properly
112
+ expect(IS_THIRDWEB_URL_CACHE.get(url)).toBe(true);
113
+ });
114
+ });
@@ -7,23 +7,14 @@ import {
7
7
  detectOS,
8
8
  detectPlatform,
9
9
  } from "./detect-platform.js";
10
+ import { isJWT } from "./jwt/is-jwt.js";
10
11
 
11
12
  const DEFAULT_REQUEST_TIMEOUT = 60000;
12
13
 
13
- const FETCH_CACHE = new WeakMap<
14
- { client: ThirdwebClient; ecosystem?: Ecosystem },
15
- (url: string, init?: RequestInit) => Promise<Response>
16
- >();
17
-
18
14
  /**
19
15
  * @internal
20
16
  */
21
17
  export function getClientFetch(client: ThirdwebClient, ecosystem?: Ecosystem) {
22
- if (FETCH_CACHE.has({ client, ecosystem })) {
23
- // biome-ignore lint/style/noNonNullAssertion: the `has` above ensures that this will always be set
24
- return FETCH_CACHE.get({ client, ecosystem })!;
25
- }
26
-
27
18
  /**
28
19
  * @internal
29
20
  */
@@ -41,7 +32,11 @@ export function getClientFetch(client: ThirdwebClient, ecosystem?: Ecosystem) {
41
32
  if (!headers) {
42
33
  headers = new Headers();
43
34
  }
44
- const authToken = getTWAuthToken();
35
+ const authToken =
36
+ client.secretKey && isJWT(client.secretKey)
37
+ ? client.secretKey
38
+ : undefined;
39
+
45
40
  // if we have an auth token set, use that (thirdweb.com/dashboard sets this for the user)
46
41
  // pay urls should never send the auth token, because we always want the "developer" to be the one making the request, not the "end user"
47
42
  if (authToken && !isPayUrl(url)) {
@@ -83,7 +78,6 @@ export function getClientFetch(client: ThirdwebClient, ecosystem?: Ecosystem) {
83
78
  }
84
79
  });
85
80
  }
86
- FETCH_CACHE.set({ client, ecosystem }, fetchWithHeaders);
87
81
  return fetchWithHeaders;
88
82
  }
89
83
 
@@ -96,7 +90,7 @@ const THIRDWEB_DOMAINS = [
96
90
  ".thirdweb-dev.com",
97
91
  ] as const;
98
92
 
99
- const IS_THIRDWEB_URL_CACHE = new LruMap<boolean>(4096);
93
+ export const IS_THIRDWEB_URL_CACHE = new LruMap<boolean>(4096);
100
94
 
101
95
  /**
102
96
  * @internal
@@ -196,16 +190,3 @@ function parseOs(os: OperatingSystem | NodeJS.Platform) {
196
190
  return osLowerCased.replace(/\s/gi, "_");
197
191
  }
198
192
  }
199
-
200
- function getTWAuthToken(): string | null {
201
- if (
202
- typeof globalThis !== "undefined" &&
203
- "TW_AUTH_TOKEN" in globalThis &&
204
- // biome-ignore lint/suspicious/noExplicitAny: get around globalThis typing
205
- typeof (globalThis as any).TW_AUTH_TOKEN === "string"
206
- ) {
207
- // biome-ignore lint/suspicious/noExplicitAny: get around globalThis typing
208
- return (globalThis as any).TW_AUTH_TOKEN as string;
209
- }
210
- return null;
211
- }
@@ -0,0 +1,5 @@
1
+ import type { JWTString } from "./types.js";
2
+
3
+ export function isJWT(str: string): str is JWTString {
4
+ return str.split(".").length === 3;
5
+ }
@@ -8,3 +8,5 @@ export type JWTPayload<Tctx = unknown> = {
8
8
  jti: string;
9
9
  ctx?: Tctx;
10
10
  };
11
+
12
+ export type JWTString = `${string}.${string}.${string}`;
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "5.57.0-nightly-cf1683c3feea6eb36ac6b902a1dd64c8ed8803c9-20240918000340";
1
+ export const version = "5.57.1";