@upeex/ads-sdk 0.1.1 → 0.1.3

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.
@@ -1,4 +1,9 @@
1
- export default function AdBanner({ client, slot }: {
2
- client: any;
3
- slot: any;
4
- }): any;
1
+ type Props = {
2
+ client: string;
3
+ slot: string;
4
+ baseUrl?: string;
5
+ theme?: 'light' | 'dark';
6
+ style?: object;
7
+ };
8
+ export default function AdBanner({ client, slot, baseUrl, theme, style, }: Props): any;
9
+ export {};
package/dist/core.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- export interface LoadAdOptions {
2
- baseUrl?: string;
1
+ type LoadAdParams = {
2
+ baseUrl: string;
3
3
  client: string;
4
4
  slot: string;
5
- }
6
- export declare function loadAd({ baseUrl, client, slot }: LoadAdOptions): Promise<any>;
7
- export declare function countView(baseUrl: string, token: string): void;
5
+ display?: string;
6
+ };
7
+ export declare function fetchAd({ baseUrl, client, slot, display, }: LoadAdParams): Promise<any>;
8
+ export declare function countView(baseUrl: string, token: string): Promise<void>;
9
+ export {};
package/dist/index.js CHANGED
@@ -1,82 +1,113 @@
1
1
  import { jsxs, jsx } from 'react/jsx-runtime';
2
- import { useState, useRef, useEffect } from 'react';
3
- import { StyleSheet, View, Text, TouchableOpacity, Image, Linking } from 'react-native';
2
+ import { useState, useEffect } from 'react';
3
+ import { Platform, Dimensions, StyleSheet, TouchableOpacity, Text, Linking } from 'react-native';
4
4
 
5
5
  async function getSignals() {
6
- let lang = 'pt-BR';
7
- let tz = 'UTC';
8
- try {
9
- const intl = Intl.DateTimeFormat().resolvedOptions();
10
- lang = intl.locale;
11
- tz = intl.timeZone;
12
- }
13
- catch (_a) { }
6
+ var _a, _b, _c, _d, _e, _f;
7
+ let platform = 'unknown';
8
+ let screenWidth = 0;
9
+ let screenHeight = 0;
14
10
  let userAgent = 'unknown';
15
- if (typeof navigator !== 'undefined' && navigator.userAgent) {
11
+ let locale = 'en-US';
12
+ let timezone = 'UTC';
13
+ if (typeof window !== 'undefined') {
14
+ // Web / PWA
15
+ platform = 'web';
16
+ screenWidth = window.innerWidth;
17
+ screenHeight = window.innerHeight;
16
18
  userAgent = navigator.userAgent;
19
+ locale =
20
+ ((_c = (_b = (_a = Intl === null || Intl === void 0 ? void 0 : Intl.DateTimeFormat) === null || _a === void 0 ? void 0 : _a.call(Intl)) === null || _b === void 0 ? void 0 : _b.resolvedOptions) === null || _c === void 0 ? void 0 : _c.call(_b).locale) || 'en-US';
21
+ timezone =
22
+ ((_f = (_e = (_d = Intl === null || Intl === void 0 ? void 0 : Intl.DateTimeFormat) === null || _d === void 0 ? void 0 : _d.call(Intl)) === null || _e === void 0 ? void 0 : _e.resolvedOptions) === null || _f === void 0 ? void 0 : _f.call(_e).timeZone) || 'UTC';
17
23
  }
18
- let screenWidth = 0;
19
- let screenHeight = 0;
20
- if (typeof window !== 'undefined' && window.screen) {
21
- screenWidth = window.screen.width;
22
- screenHeight = window.screen.height;
24
+ else if (typeof Platform !== 'undefined') {
25
+ // React Native
26
+ platform = Platform.OS;
27
+ const dim = Dimensions.get('window');
28
+ screenWidth = dim.width;
29
+ screenHeight = dim.height;
30
+ // RN não tem userAgent nativo
23
31
  }
24
32
  return {
25
- isMobile: true,
33
+ platform,
26
34
  screenWidth,
27
35
  screenHeight,
28
36
  userAgent,
29
- lang,
30
- tz,
31
- href: typeof location !== 'undefined' ? location.href : 'app://mobile',
32
- hostname: typeof location !== 'undefined' ? location.hostname : 'mobile-app'
37
+ locale,
38
+ timezone,
33
39
  };
34
40
  }
35
41
 
36
- async function loadAd({ baseUrl = 'http://localhost:3000', client, slot }) {
42
+ async function fetchAd({ baseUrl, client, slot, display = 'BANNER', }) {
37
43
  const signals = await getSignals();
38
- const makeRes = await fetch(`${baseUrl}/ad/make/mobile`, {
44
+ const res = await fetch(`${baseUrl}/make`, {
39
45
  method: 'POST',
40
- headers: {
41
- 'Content-Type': 'application/json'
42
- },
46
+ headers: { 'Content-Type': 'application/json' },
43
47
  body: JSON.stringify({
44
48
  client,
45
49
  slot,
46
- display: 'BANNER',
47
- responsive: 1,
48
- ...signals
49
- })
50
+ display,
51
+ isMobile: signals.platform !== 'web' ? 1 : 0,
52
+ ...signals,
53
+ }),
50
54
  });
51
- if (!makeRes.ok) {
52
- throw new Error('Erro ao chamar /ad/make/mobile');
53
- }
54
- const makeData = await makeRes.json();
55
- if (!makeData.url) {
56
- throw new Error('MAKE não retornou URL');
55
+ if (!res.ok)
56
+ return null;
57
+ return res.json();
58
+ }
59
+ async function countView(baseUrl, token) {
60
+ try {
61
+ await fetch(`${baseUrl}/count/views`, {
62
+ method: 'POST',
63
+ headers: { 'Content-Type': 'application/json' },
64
+ body: JSON.stringify({ token }),
65
+ });
57
66
  }
58
- const showRes = await fetch(makeData.url);
59
- return showRes.json();
67
+ catch (_a) { }
60
68
  }
61
69
 
62
- function AdBanner({ client, slot }) {
70
+ function AdBanner({ client, slot, baseUrl = 'https://ads.upeex.com.br', theme = 'light', style = {}, }) {
63
71
  const [ad, setAd] = useState(null);
64
- useRef(false);
65
72
  useEffect(() => {
66
- loadAd({
67
- baseUrl: 'http://localhost:3000',
68
- client,
69
- slot
70
- }).then(setAd);
71
- }, [client, slot]);
73
+ fetchAd({ baseUrl, client, slot }).then((data) => {
74
+ if (!data)
75
+ return;
76
+ setAd(data);
77
+ countView(baseUrl, data.adtoken);
78
+ });
79
+ }, [client, slot, baseUrl]);
72
80
  if (!ad)
73
81
  return null;
74
- return (jsxs(View, { style: styles.container, children: [jsx(Text, { style: styles.label, children: "Promovido" }), jsx(TouchableOpacity, { onPress: () => Linking.openURL(ad.click_url), children: jsx(Image, { source: { uri: ad.image }, style: styles.banner, resizeMode: "contain" }) })] }));
82
+ const handlePress = () => {
83
+ if (Platform.OS === 'web') {
84
+ window.open(ad.url, '_blank');
85
+ }
86
+ else {
87
+ Linking.openURL(ad.url);
88
+ }
89
+ };
90
+ return (jsxs(TouchableOpacity, { style: [styles.container, theme === 'dark' && styles.dark, style], onPress: handlePress, children: [jsx(Text, { style: styles.title, children: "Publicidade" }), jsx(Text, { style: styles.text, children: ad.url })] }));
75
91
  }
76
92
  const styles = StyleSheet.create({
77
- container: { width: '100%', alignItems: 'center', marginVertical: 8 },
78
- label: { fontSize: 10, color: '#777', marginBottom: 2 },
79
- banner: { width: '100%', height: 50 }
93
+ container: {
94
+ backgroundColor: '#f2f2f2',
95
+ padding: 12,
96
+ borderTopWidth: 1,
97
+ borderColor: '#ddd',
98
+ },
99
+ dark: {
100
+ backgroundColor: '#121212',
101
+ },
102
+ title: {
103
+ fontSize: 12,
104
+ fontWeight: '600',
105
+ marginBottom: 4,
106
+ },
107
+ text: {
108
+ fontSize: 11,
109
+ color: '#555',
110
+ },
80
111
  });
81
112
 
82
113
  export { AdBanner };
package/dist/signals.d.ts CHANGED
@@ -1,10 +1,8 @@
1
1
  export declare function getSignals(): Promise<{
2
- isMobile: boolean;
2
+ platform: string;
3
3
  screenWidth: number;
4
4
  screenHeight: number;
5
5
  userAgent: string;
6
- lang: string;
7
- tz: string;
8
- href: string;
9
- hostname: string;
6
+ locale: string;
7
+ timezone: string;
10
8
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@upeex/ads-sdk",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
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",
@@ -21,7 +21,8 @@
21
21
  "react-native",
22
22
  "expo",
23
23
  "sdk",
24
- "upeex"
24
+ "upeex",
25
+ "upee"
25
26
  ],
26
27
  "author": "Upeex",
27
28
  "license": "MIT",