@upeex/ads-sdk 1.1.6 → 1.1.8

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 (2) hide show
  1. package/dist/index.js +77 -76
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { jsx, jsxs } from 'react/jsx-runtime';
1
+ import { jsx } from 'react/jsx-runtime';
2
2
  import { useState, useEffect } from 'react';
3
3
  import { Platform, Dimensions, StyleSheet, View, Text, TouchableOpacity, Image, Linking } from 'react-native';
4
4
 
@@ -40,44 +40,43 @@ async function getSignals() {
40
40
  }
41
41
 
42
42
  async function fetchAd({ baseUrl, client, slot, display = 'BANNER', debug = false, }) {
43
+ const log = (...args) => {
44
+ if (debug)
45
+ console.log('[UPEEX ADS]', ...args);
46
+ };
43
47
  const signals = await getSignals();
44
- if (debug) {
45
- console.log('[ADS] signals:', signals);
46
- }
47
- // 1️⃣ chama o make
48
- const res = await fetch(`${baseUrl}/make/mobile`, {
48
+ log('signals', signals);
49
+ // 1️⃣ MAKE
50
+ const makeRes = await fetch(`${baseUrl}/ad/make/mobile`, {
49
51
  method: 'POST',
50
52
  headers: { 'Content-Type': 'application/json' },
51
53
  body: JSON.stringify({
52
54
  client,
53
55
  slot,
54
56
  display,
55
- isMobile: signals.platform !== 'web' ? 1 : 0,
56
57
  ...signals,
57
58
  }),
58
59
  });
59
- if (!res.ok) {
60
- if (debug)
61
- console.error('[ADS] make error:', res.status);
60
+ if (!makeRes.ok) {
61
+ log('Erro no make', makeRes.status);
62
62
  return null;
63
63
  }
64
- // make retorna UMA URL
65
- const showUrl = await res.text();
66
- if (debug) {
67
- console.log('[ADS] showUrl:', showUrl);
68
- }
69
- // 2️⃣ faz GET nessa URL
70
- const adRes = await fetch(showUrl);
71
- if (!adRes.ok) {
72
- if (debug)
73
- console.error('[ADS] show error:', adRes.status);
64
+ const makeData = await makeRes.json();
65
+ log('make response', makeData);
66
+ if (!makeData.url) {
67
+ log('make não retornou url');
74
68
  return null;
75
69
  }
76
- const adData = await adRes.json();
77
- if (debug) {
78
- console.log('[ADS] adData:', adData);
70
+ // 2️⃣ SHOW
71
+ log('fazendo fetch do showUrl', makeData.url);
72
+ const showRes = await fetch(makeData.url);
73
+ if (!showRes.ok) {
74
+ log('Erro no show', showRes.status);
75
+ return null;
79
76
  }
80
- return adData;
77
+ const ad = await showRes.json();
78
+ log('ad recebido', ad);
79
+ return ad;
81
80
  }
82
81
 
83
82
  function AdBanner({ client, slot, baseUrl = 'https://app.upeex.com.br/ad', theme = 'light', style = {}, debug = false, }) {
@@ -87,51 +86,52 @@ function AdBanner({ client, slot, baseUrl = 'https://app.upeex.com.br/ad', theme
87
86
  if (debug)
88
87
  console.log('[UPEEX ADS]', ...args);
89
88
  };
90
- async function loadAd() {
91
- try {
92
- log('Carregando anúncio', { client, slot, baseUrl });
93
- const data = await fetchAd({
94
- baseUrl,
95
- client,
96
- slot,
97
- debug,
98
- });
99
- log('Resposta do anúncio', data);
100
- if (!(data === null || data === void 0 ? void 0 : data.image) || !(data === null || data === void 0 ? void 0 : data.clickUrl)) {
101
- setError('Anúncio inválido ou vazio');
89
+ useEffect(() => {
90
+ let refreshTimer;
91
+ const load = async () => {
92
+ try {
93
+ log('Buscando anúncio', { client, slot });
94
+ const data = await fetchAd({ baseUrl, client, slot, debug });
95
+ log('Resposta do servidor', data);
96
+ if (!data || !data.image) {
97
+ setError('Nenhum anúncio disponível');
98
+ setAd(null);
99
+ return;
100
+ }
101
+ setAd(data);
102
+ setError(null);
103
+ if (data.refresh && data.refresh > 0) {
104
+ log(`Refresh ativo: ${data.refresh}s`);
105
+ refreshTimer = setTimeout(() => {
106
+ log('Refazendo request do anúncio');
107
+ load();
108
+ }, data.refresh * 1000);
109
+ }
110
+ }
111
+ catch (err) {
112
+ console.error('[UPEEX ADS] Erro ao carregar anúncio', err);
113
+ setError('Erro ao carregar anúncio');
102
114
  setAd(null);
103
- return;
104
115
  }
105
- setAd(data);
106
- setError(null);
107
- }
108
- catch (err) {
109
- console.error('[UPEEX ADS] Erro ao carregar anúncio', err);
110
- setError('Erro ao carregar anúncio');
111
- setAd(null);
112
- }
113
- }
114
- useEffect(() => {
115
- if (!client || !slot) {
116
- log('client ou slot não informado');
117
- return;
116
+ };
117
+ if (client && slot) {
118
+ load();
118
119
  }
119
- loadAd();
120
+ return () => {
121
+ if (refreshTimer)
122
+ clearTimeout(refreshTimer);
123
+ };
120
124
  }, [client, slot, baseUrl]);
121
- // 🔁 auto refresh (se backend enviar)
122
- useEffect(() => {
123
- if (!(ad === null || ad === void 0 ? void 0 : ad.refresh))
124
- return;
125
- log('Auto refresh em', ad.refresh, 'segundos');
126
- const timer = setTimeout(loadAd, ad.refresh * 1000);
127
- return () => clearTimeout(timer);
128
- }, [ad === null || ad === void 0 ? void 0 : ad.refresh]);
129
- // ❌ nada pra renderizar
125
+ if (error) {
126
+ return debug ? (jsx(View, { style: [styles.container, styles.debug], children: jsx(Text, { style: styles.debugText, children: error }) })) : null;
127
+ }
130
128
  if (!ad) {
131
- return debug ? (jsx(View, { style: [styles.container, styles.debug], children: jsx(Text, { style: styles.debugText, children: error !== null && error !== void 0 ? error : 'Nenhum anúncio carregado' }) })) : null;
129
+ return debug ? (jsx(View, { style: [styles.container, styles.loading], children: jsx(Text, { style: styles.loadingText, children: "Carregando an\u00FAncio\u2026" }) })) : null;
132
130
  }
133
131
  const handlePress = () => {
134
132
  log('Clique no anúncio', ad.clickUrl);
133
+ if (!ad.clickUrl)
134
+ return;
135
135
  if (Platform.OS === 'web') {
136
136
  window.open(ad.clickUrl, '_blank');
137
137
  }
@@ -139,38 +139,39 @@ function AdBanner({ client, slot, baseUrl = 'https://app.upeex.com.br/ad', theme
139
139
  Linking.openURL(ad.clickUrl);
140
140
  }
141
141
  };
142
- return (jsxs(TouchableOpacity, { activeOpacity: 0.9, onPress: handlePress, style: [
142
+ return (jsx(TouchableOpacity, { activeOpacity: 0.9, style: [
143
143
  styles.container,
144
144
  theme === 'dark' && styles.dark,
145
145
  style,
146
- ], children: [jsx(Image, { source: { uri: ad.image }, style: {
147
- width: ad.width || 320,
148
- height: ad.height || 50,
149
- }, resizeMode: "contain" }), debug && (jsxs(Text, { style: styles.debugInfo, children: ["client: ", client, " | slot: ", slot] }))] }));
146
+ ], onPress: handlePress, children: jsx(Image, { source: { uri: ad.image }, style: {
147
+ width: ad.width || 320,
148
+ height: ad.height || 50,
149
+ resizeMode: 'contain',
150
+ } }) }));
150
151
  }
151
152
  const styles = StyleSheet.create({
152
153
  container: {
154
+ backgroundColor: '#f2f2f2',
155
+ padding: 8,
153
156
  alignItems: 'center',
154
157
  justifyContent: 'center',
155
- backgroundColor: '#f2f2f2',
156
- paddingVertical: 4,
157
158
  },
158
159
  dark: {
159
160
  backgroundColor: '#121212',
160
161
  },
162
+ loading: {
163
+ backgroundColor: '#fafafa',
164
+ },
165
+ loadingText: {
166
+ fontSize: 11,
167
+ color: '#999',
168
+ },
161
169
  debug: {
162
- padding: 10,
163
- backgroundColor: '#fff3f3',
170
+ backgroundColor: '#ffecec',
164
171
  },
165
172
  debugText: {
166
173
  fontSize: 11,
167
174
  color: '#c00',
168
- textAlign: 'center',
169
- },
170
- debugInfo: {
171
- marginTop: 4,
172
- fontSize: 9,
173
- color: '#888',
174
175
  },
175
176
  });
176
177
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@upeex/ads-sdk",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Upeex Ads SDK for React Native and universal apps",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",