@adland/react 0.7.2 → 0.8.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @adland/react
2
2
 
3
+ ## 0.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - df8ba42: handle errors from api for no ad
8
+
3
9
  ## 0.7.2
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adland/react",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -15,6 +15,11 @@
15
15
  "import": "./dist/index.js",
16
16
  "require": "./dist/index.cjs"
17
17
  },
18
+ "./types": {
19
+ "types": "./dist/types.d.ts",
20
+ "import": "./dist/types.js",
21
+ "require": "./dist/types.cjs"
22
+ },
18
23
  "./package.json": "./package.json"
19
24
  },
20
25
  "peerDependencies": {
@@ -8,48 +8,9 @@ import BasicAdBody from "./BasicAdBody";
8
8
  import LinkAdContent from "./content/LinkAdContent";
9
9
  import MiniappAdContent from "./content/MiniappAdContent";
10
10
  import CastAdContent from "./content/CastAdContent";
11
-
12
- type Network = "testnet" | "mainnet";
13
-
14
- /**
15
- * Get the base URL for the network
16
- * Uses relative URL in browser (for local dev), otherwise defaults to production
17
- */
18
- const getBaseUrl = (network: Network): string => {
19
- if (typeof window !== "undefined") {
20
- // In browser - use relative URL for local development
21
- return "";
22
- }
23
-
24
- if (network === "testnet") {
25
- return "https://testnet.adland.space";
26
- }
27
-
28
- if (network === "mainnet") {
29
- return "https://app.adland.space";
30
- }
31
-
32
- return "";
33
- };
34
-
35
- export interface AdProps extends React.HTMLAttributes<HTMLDivElement> {
36
- /**
37
- * The owner/creator of the ad
38
- */
39
- owner: string;
40
- /**
41
- * Unique identifier for the ad
42
- */
43
- slotId: string;
44
- /**
45
- * Network to use for tracking requests (currently only "testnet" is supported)
46
- */
47
- network?: Network;
48
- /**
49
- * Optional base URL override. If not provided, uses relative URL in browser or production URL in SSR
50
- */
51
- baseUrl?: string;
52
- }
11
+ import { AdDataQueryError, AdProps } from "../types";
12
+ import { getBaseUrl } from "../utils";
13
+ import { adlandApiUrl } from "../utils/constants";
53
14
 
54
15
  /**
55
16
  * Simple Ad component with built-in view and click tracking
@@ -78,7 +39,7 @@ export function Ad({
78
39
  `ad-data-${owner}-${slotId}`,
79
40
  async () => {
80
41
  const res = await fetch(
81
- `${networkBaseUrl}/api/data/owner/${owner.toLowerCase()}/slot/${slotId}`,
42
+ `${adlandApiUrl}/api/data/owner/${owner.toLowerCase()}/slot/${slotId}`,
82
43
  {
83
44
  method: "GET",
84
45
  headers: {
@@ -86,7 +47,18 @@ export function Ad({
86
47
  },
87
48
  },
88
49
  );
89
- return res.json();
50
+
51
+ if (!res.ok) {
52
+ throw new Error(AdDataQueryError.NO_AD);
53
+ }
54
+
55
+ const data = await res.json();
56
+
57
+ if (data.error) {
58
+ throw new Error(data.error);
59
+ }
60
+
61
+ return data;
90
62
  },
91
63
  {
92
64
  enabled: !!owner && !!slotId,
@@ -94,6 +66,8 @@ export function Ad({
94
66
  );
95
67
  const networkBaseUrl = baseUrl ?? getBaseUrl(network);
96
68
 
69
+ console.log({ error, adData });
70
+
97
71
  const send = useCallback(
98
72
  (type: "view" | "click") => {
99
73
  const trackEndpoint = `${networkBaseUrl}/api/analytics/track`;
@@ -165,6 +139,16 @@ export function Ad({
165
139
  return <LoadingAdContent />;
166
140
  }
167
141
  if (error) {
142
+ // @ts-ignore
143
+ if (error instanceof Error) {
144
+ if (error.message === AdDataQueryError.NO_AD) {
145
+ return (
146
+ <EmtpyAdContent
147
+ data={{ url: networkBaseUrl + `${owner}/${slotId}` }}
148
+ />
149
+ );
150
+ }
151
+ }
168
152
  return <ErrorAdContent error={error} />;
169
153
  }
170
154
  if (adData) {
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // Main exports
2
2
  export { Ad } from "./components/Ad";
3
- export type { AdProps } from "./components/Ad";
3
+ export type { AdProps, AdDataQueryResponse, AdDataQueryError } from "./types";
4
4
 
5
5
  // Utility exports
6
6
  export {
package/src/types.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { AdData } from "@adland/data";
2
+
3
+ export type Network = "testnet" | "mainnet";
4
+
5
+ export interface AdProps extends React.HTMLAttributes<HTMLDivElement> {
6
+ /**
7
+ * The owner/creator of the ad
8
+ */
9
+ owner: string;
10
+ /**
11
+ * Unique identifier for the ad
12
+ */
13
+ slotId: string;
14
+ /**
15
+ * Network to use for tracking requests (currently only "testnet" is supported)
16
+ */
17
+ network?: Network;
18
+ /**
19
+ * Optional base URL override. If not provided, uses relative URL in browser or production URL in SSR
20
+ */
21
+ baseUrl?: string;
22
+ }
23
+
24
+ export enum AdDataQueryError {
25
+ NO_AD = "NO_AD",
26
+ }
27
+
28
+ export interface AdDataQueryResponse {
29
+ error?: AdDataQueryError;
30
+ data?: AdData;
31
+ }
@@ -12,3 +12,8 @@ export const adCardIcon: Record<
12
12
  cast: MessageCircle,
13
13
  miniapp: LayoutGrid,
14
14
  };
15
+
16
+ export const adlandApiUrl =
17
+ process.env.NODE_ENV === "development"
18
+ ? "http://localhost:3069"
19
+ : "https://api.adland.space";
@@ -0,0 +1,22 @@
1
+ import { Network } from "../types";
2
+
3
+ /**
4
+ * Get the base URL for the network
5
+ * Uses relative URL in browser (for local dev), otherwise defaults to production
6
+ */
7
+ export const getBaseUrl = (network: Network): string => {
8
+ if (typeof window !== "undefined") {
9
+ // In browser - use relative URL for local development
10
+ return "";
11
+ }
12
+
13
+ if (network === "testnet") {
14
+ return "https://testnet.adland.space";
15
+ }
16
+
17
+ if (network === "mainnet") {
18
+ return "https://app.adland.space";
19
+ }
20
+
21
+ return "";
22
+ };
package/tsup.config.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  import { defineConfig } from "tsup";
2
2
 
3
3
  export default defineConfig({
4
- entry: ["src/index.ts"],
4
+ entry: {
5
+ index: "src/index.ts",
6
+ types: "src/types.ts",
7
+ },
5
8
  format: ["cjs", "esm"],
6
9
  dts: true,
7
10
  splitting: false,