@chimera-pe/react-saas 3.0.0 → 3.0.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.
@@ -0,0 +1,322 @@
1
+ import { jsxs as g, jsx as a } from "react/jsx-runtime";
2
+ import { useEffect as m, useContext as k } from "react";
3
+ import o from "prop-types";
4
+ import { AuthProvider as q, AuthContext as x } from "react-oauth2-code-pkce";
5
+ import { create as w } from "zustand";
6
+ import { useTranslate as f, I18n as I } from "react-polyglot";
7
+ import { union as U } from "lodash";
8
+ import C from "navigator-languages";
9
+ const O = "https://saas.chimera.com.pe/backend/v3", R = "https://saas.chimera.com.pe/oauth/v3", A = "http://localhost:7000", T = "http://localhost:7777", u = {
10
+ primary: "#1C6CCC",
11
+ secondary: "#17A7FF",
12
+ info: "#2196f3",
13
+ success: "#4caf50",
14
+ error: "#f44336",
15
+ warning: "#ff9800"
16
+ }, d = w((e) => ({
17
+ inicializado: !1,
18
+ estado: {
19
+ inicializando: !0,
20
+ error: null
21
+ },
22
+ instancia: {
23
+ identificador: null,
24
+ nombre: null,
25
+ abreviatura: null,
26
+ requiereLogin: !1,
27
+ logo: null,
28
+ color: u
29
+ },
30
+ idioma: "en",
31
+ inicializar: async (i, r) => {
32
+ e({
33
+ estado: {
34
+ inicializando: !0,
35
+ error: null
36
+ }
37
+ });
38
+ try {
39
+ const t = await fetch(`${i || O}/identidad/?codigoAplicacion=${r}`);
40
+ if (!t.ok)
41
+ throw new Error(`inicializar.error.${t.status}`);
42
+ const n = await t.json();
43
+ e({
44
+ inicializado: !0,
45
+ estado: {
46
+ inicializando: !1,
47
+ error: null
48
+ },
49
+ instancia: {
50
+ identificador: n.identificador,
51
+ nombre: n.nombre,
52
+ abreviatura: n.nombre.match(/\b([A-Z])/g).join(""),
53
+ requiereLogin: n.requiereLogin,
54
+ logo: n.logo,
55
+ color: {
56
+ ...u,
57
+ ...n.color
58
+ }
59
+ }
60
+ });
61
+ } catch (t) {
62
+ e({
63
+ inicializado: !1,
64
+ estado: {
65
+ inicializando: !1,
66
+ error: t.message
67
+ },
68
+ instancia: {
69
+ identificador: null,
70
+ nombre: null,
71
+ abreviatura: null,
72
+ requiereLogin: !1,
73
+ logo: null,
74
+ color: u
75
+ }
76
+ });
77
+ }
78
+ },
79
+ cambiarIdioma: (i) => {
80
+ e({
81
+ idioma: i
82
+ });
83
+ }
84
+ })), E = "data:image/svg+xml,%3csvg%20width='24'%20height='24'%20stroke='%23000'%20viewBox='0%200%2024%2024'%20xmlns='http://www.w3.org/2000/svg'%3e%3cstyle%3e.spinner_V8m1{transform-origin:center;animation:spinner_zKoa%202s%20linear%20infinite}.spinner_V8m1%20circle{stroke-linecap:round;animation:spinner_YpZS%201.5s%20ease-in-out%20infinite}@keyframes%20spinner_zKoa{100%25{transform:rotate(360deg)}}@keyframes%20spinner_YpZS{0%25{stroke-dasharray:0%20150;stroke-dashoffset:0}47.5%25{stroke-dasharray:42%20150;stroke-dashoffset:-16}95%25,100%25{stroke-dasharray:42%20150;stroke-dashoffset:-59}}%3c/style%3e%3cg%20class='spinner_V8m1'%3e%3ccircle%20cx='12'%20cy='12'%20r='9.5'%20fill='none'%20stroke-width='3'%3e%3c/circle%3e%3c/g%3e%3c/svg%3e", L = () => {
85
+ const e = f();
86
+ return /* @__PURE__ */ g(
87
+ "div",
88
+ {
89
+ style: {
90
+ display: "flex",
91
+ gap: "0.5rem"
92
+ },
93
+ children: [
94
+ /* @__PURE__ */ a(
95
+ "img",
96
+ {
97
+ src: E,
98
+ alt: e("saas.cargando"),
99
+ style: {
100
+ filter: "invert(99%) sepia(2%) saturate(740%) hue-rotate(250deg) brightness(93%) contrast(93%)"
101
+ }
102
+ }
103
+ ),
104
+ /* @__PURE__ */ a("span", { children: e("saas.cargando") })
105
+ ]
106
+ }
107
+ );
108
+ }, h = ({ titulo: e, texto: i }) => {
109
+ const r = f();
110
+ return /* @__PURE__ */ g(
111
+ "div",
112
+ {
113
+ style: {
114
+ borderWidth: "2px",
115
+ borderStyle: "solid",
116
+ borderRadius: "7px",
117
+ backgroundColor: "#f44336",
118
+ padding: "0.5rem",
119
+ borderColor: "#8d1108"
120
+ },
121
+ children: [
122
+ /* @__PURE__ */ a(
123
+ "h4",
124
+ {
125
+ style: {
126
+ marginTop: 0,
127
+ marginBottom: "0.3rem"
128
+ },
129
+ children: r(e)
130
+ }
131
+ ),
132
+ i && /* @__PURE__ */ a("p", { children: r(i) })
133
+ ]
134
+ }
135
+ );
136
+ };
137
+ h.propTypes = {
138
+ titulo: o.string.isRequired,
139
+ texto: o.string
140
+ };
141
+ const b = ({ devAuthUrl: e, redirectUri: i, children: r }) => {
142
+ const t = d((n) => n.instancia);
143
+ if (t.requiereLogin) {
144
+ const n = e || R;
145
+ return /* @__PURE__ */ a(
146
+ q,
147
+ {
148
+ authConfig: {
149
+ clientId: t.identificador,
150
+ authorizationEndpoint: `${n}/oauth2/authorize`,
151
+ tokenEndpoint: `${n}/oauth2/token`,
152
+ logoutEndpoint: `${n}/connect/logout`,
153
+ redirectUri: i,
154
+ scope: "openid profile"
155
+ },
156
+ children: r
157
+ }
158
+ );
159
+ }
160
+ return r;
161
+ };
162
+ b.propTypes = {
163
+ devAuthUrl: o.string,
164
+ redirectUri: o.string.isRequired,
165
+ children: o.element.isRequired
166
+ };
167
+ const v = ({ aplicacion: e, devBackendUrl: i }) => {
168
+ const r = d((n) => n.estado), t = d((n) => n.inicializar);
169
+ return m(() => {
170
+ t(i, e);
171
+ }, [t, i, e]), /* @__PURE__ */ a(
172
+ "div",
173
+ {
174
+ style: {
175
+ display: "flex",
176
+ flexDirection: "column",
177
+ alignItems: "center",
178
+ justifyContent: "center",
179
+ minHeight: "100vh",
180
+ backgroundColor: "#141316",
181
+ color: "#e6e1e6"
182
+ },
183
+ children: r.inicializando ? /* @__PURE__ */ a(L, {}) : r.error && /* @__PURE__ */ a(h, { titulo: "saas.error.titulo", texto: r.error })
184
+ }
185
+ );
186
+ };
187
+ v.propTypes = {
188
+ aplicacion: o.string.isRequired,
189
+ devBackendUrl: o.string
190
+ };
191
+ const y = ({
192
+ aplicacion: e,
193
+ devBackendUrl: i,
194
+ devAuthUrl: r,
195
+ redirectUri: t,
196
+ children: n
197
+ }) => d((l) => l.inicializado) ? /* @__PURE__ */ a(
198
+ b,
199
+ {
200
+ devAuthUrl: r,
201
+ redirectUri: t,
202
+ children: n
203
+ }
204
+ ) : /* @__PURE__ */ a(
205
+ v,
206
+ {
207
+ aplicacion: e,
208
+ devBackendUrl: i
209
+ }
210
+ );
211
+ y.propTypes = {
212
+ aplicacion: o.string.isRequired,
213
+ devBackendUrl: o.string,
214
+ devAuthUrl: o.string,
215
+ redirectUri: o.string.isRequired,
216
+ children: o.element.isRequired
217
+ };
218
+ const _ = {
219
+ cargando: "Loading...",
220
+ error: {
221
+ titulo: "Could not initialize application"
222
+ }
223
+ }, B = {
224
+ cargando: "Cargando...",
225
+ error: {
226
+ titulo: "Error iniciando aplicación"
227
+ }
228
+ }, p = {
229
+ en: _,
230
+ es: B
231
+ }, z = ({ mensajes: e, children: i }) => {
232
+ const r = d((t) => t.idioma);
233
+ return /* @__PURE__ */ a(I, { locale: r, messages: e[r], children: i });
234
+ };
235
+ z.propTypes = {
236
+ mensajes: o.object,
237
+ children: o.element.isRequired
238
+ };
239
+ const S = () => {
240
+ const e = C();
241
+ if (!(e != null && e.length))
242
+ return;
243
+ const i = e[0];
244
+ return i.indexOf("-") ? i.substring(0, i.indexOf("-")) : i;
245
+ }, j = ({ mensajes: e, idiomaDefecto: i, children: r }) => {
246
+ const t = d((s) => s.cambiarIdioma), n = S(), c = U(Object.keys(e), Object.keys(p));
247
+ m(() => {
248
+ let s = c[0];
249
+ n && Object.hasOwn(e, n) ? s = n : i && Object.hasOwn(e, i) && (s = i), t(s);
250
+ }, [c, e, i, n, t]);
251
+ const l = {};
252
+ return c.forEach((s) => {
253
+ l[s] = {
254
+ ...Object.hasOwn(e, s) ? e[s] : {},
255
+ saas: Object.hasOwn(p, s) ? p[s] : {}
256
+ };
257
+ }), /* @__PURE__ */ a(z, { mensajes: l, children: r });
258
+ };
259
+ j.propTypes = {
260
+ mensajes: o.object,
261
+ idiomaDefecto: o.string,
262
+ children: o.element.isRequired
263
+ };
264
+ const P = ({
265
+ aplicacion: e,
266
+ devBackendUrl: i = A,
267
+ devAuthUrl: r = T,
268
+ dev: t = !1,
269
+ redirectUri: n,
270
+ idiomaDefecto: c = "en",
271
+ mensajes: l = {
272
+ en: {},
273
+ es: {}
274
+ },
275
+ children: s
276
+ }) => /* @__PURE__ */ a(j, { mensajes: l, children: /* @__PURE__ */ a(
277
+ y,
278
+ {
279
+ aplicacion: e,
280
+ devBackendUrl: t && i,
281
+ devAuthUrl: t && r,
282
+ redirectUri: n,
283
+ idiomaDefecto: c,
284
+ children: s
285
+ }
286
+ ) });
287
+ P.propTypes = {
288
+ aplicacion: o.string.isRequired,
289
+ devBackendUrl: o.string,
290
+ devAuthUrl: o.string,
291
+ dev: o.bool,
292
+ redirectUri: o.string.isRequired,
293
+ idiomaDefecto: o.string,
294
+ mensajes: o.object,
295
+ children: o.element.isRequired
296
+ };
297
+ const W = () => {
298
+ const {
299
+ loginInProgress: e,
300
+ token: i,
301
+ tokenData: r,
302
+ logIn: t,
303
+ logOut: n,
304
+ error: c
305
+ } = k(x);
306
+ return {
307
+ loginInProgress: e,
308
+ token: i,
309
+ logIn: t,
310
+ logOut: n,
311
+ error: c,
312
+ correo: r == null ? void 0 : r.sub,
313
+ nombre: r == null ? void 0 : r.name,
314
+ perfiles: r == null ? void 0 : r.profiles
315
+ };
316
+ };
317
+ export {
318
+ y as Inicializar,
319
+ P as SaasApp,
320
+ W as useAuth,
321
+ d as useSaasStore
322
+ };
@@ -0,0 +1 @@
1
+ (function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("react/jsx-runtime"),require("react"),require("prop-types"),require("react-oauth2-code-pkce"),require("zustand"),require("react-polyglot"),require("lodash"),require("navigator-languages")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react","prop-types","react-oauth2-code-pkce","zustand","react-polyglot","lodash","navigator-languages"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s["react-saas"]={},s.ReactJSX,s.React,s.PropTypes,s.ReactOauth2CodePkce,s.Zustand,s.ReactPolyglot,s.Lodash,s.NavigatorLanguages))})(this,function(s,o,g,t,v,x,f,I,U){"use strict";const A="https://saas.chimera.com.pe/backend/v3",C="https://saas.chimera.com.pe/oauth/v3",O="http://localhost:7000",S="http://localhost:7777",h={primary:"#1C6CCC",secondary:"#17A7FF",info:"#2196f3",success:"#4caf50",error:"#f44336",warning:"#ff9800"},l=x.create(e=>({inicializado:!1,estado:{inicializando:!0,error:null},instancia:{identificador:null,nombre:null,abreviatura:null,requiereLogin:!1,logo:null,color:h},idioma:"en",inicializar:async(r,i)=>{e({estado:{inicializando:!0,error:null}});try{const a=await fetch(`${r||A}/identidad/?codigoAplicacion=${i}`);if(!a.ok)throw new Error(`inicializar.error.${a.status}`);const n=await a.json();e({inicializado:!0,estado:{inicializando:!1,error:null},instancia:{identificador:n.identificador,nombre:n.nombre,abreviatura:n.nombre.match(/\b([A-Z])/g).join(""),requiereLogin:n.requiereLogin,logo:n.logo,color:{...h,...n.color}}})}catch(a){e({inicializado:!1,estado:{inicializando:!1,error:a.message},instancia:{identificador:null,nombre:null,abreviatura:null,requiereLogin:!1,logo:null,color:h}})}},cambiarIdioma:r=>{e({idioma:r})}})),y="data:image/svg+xml,%3csvg%20width='24'%20height='24'%20stroke='%23000'%20viewBox='0%200%2024%2024'%20xmlns='http://www.w3.org/2000/svg'%3e%3cstyle%3e.spinner_V8m1{transform-origin:center;animation:spinner_zKoa%202s%20linear%20infinite}.spinner_V8m1%20circle{stroke-linecap:round;animation:spinner_YpZS%201.5s%20ease-in-out%20infinite}@keyframes%20spinner_zKoa{100%25{transform:rotate(360deg)}}@keyframes%20spinner_YpZS{0%25{stroke-dasharray:0%20150;stroke-dashoffset:0}47.5%25{stroke-dasharray:42%20150;stroke-dashoffset:-16}95%25,100%25{stroke-dasharray:42%20150;stroke-dashoffset:-59}}%3c/style%3e%3cg%20class='spinner_V8m1'%3e%3ccircle%20cx='12'%20cy='12'%20r='9.5'%20fill='none'%20stroke-width='3'%3e%3c/circle%3e%3c/g%3e%3c/svg%3e",E=()=>{const e=f.useTranslate();return o.jsxs("div",{style:{display:"flex",gap:"0.5rem"},children:[o.jsx("img",{src:y,alt:e("saas.cargando"),style:{filter:"invert(99%) sepia(2%) saturate(740%) hue-rotate(250deg) brightness(93%) contrast(93%)"}}),o.jsx("span",{children:e("saas.cargando")})]})},b=({titulo:e,texto:r})=>{const i=f.useTranslate();return o.jsxs("div",{style:{borderWidth:"2px",borderStyle:"solid",borderRadius:"7px",backgroundColor:"#f44336",padding:"0.5rem",borderColor:"#8d1108"},children:[o.jsx("h4",{style:{marginTop:0,marginBottom:"0.3rem"},children:i(e)}),r&&o.jsx("p",{children:i(r)})]})};b.propTypes={titulo:t.string.isRequired,texto:t.string};const q=({devAuthUrl:e,redirectUri:r,children:i})=>{const a=l(n=>n.instancia);if(a.requiereLogin){const n=e||C;return o.jsx(v.AuthProvider,{authConfig:{clientId:a.identificador,authorizationEndpoint:`${n}/oauth2/authorize`,tokenEndpoint:`${n}/oauth2/token`,logoutEndpoint:`${n}/connect/logout`,redirectUri:r,scope:"openid profile"},children:i})}return i};q.propTypes={devAuthUrl:t.string,redirectUri:t.string.isRequired,children:t.element.isRequired};const z=({aplicacion:e,devBackendUrl:r})=>{const i=l(n=>n.estado),a=l(n=>n.inicializar);return g.useEffect(()=>{a(r,e)},[a,r,e]),o.jsx("div",{style:{display:"flex",flexDirection:"column",alignItems:"center",justifyContent:"center",minHeight:"100vh",backgroundColor:"#141316",color:"#e6e1e6"},children:i.inicializando?o.jsx(E,{}):i.error&&o.jsx(b,{titulo:"saas.error.titulo",texto:i.error})})};z.propTypes={aplicacion:t.string.isRequired,devBackendUrl:t.string};const m=({aplicacion:e,devBackendUrl:r,devAuthUrl:i,redirectUri:a,children:n})=>l(u=>u.inicializado)?o.jsx(q,{devAuthUrl:i,redirectUri:a,children:n}):o.jsx(z,{aplicacion:e,devBackendUrl:r});m.propTypes={aplicacion:t.string.isRequired,devBackendUrl:t.string,devAuthUrl:t.string,redirectUri:t.string.isRequired,children:t.element.isRequired};const p={en:{cargando:"Loading...",error:{titulo:"Could not initialize application"}},es:{cargando:"Cargando...",error:{titulo:"Error iniciando aplicación"}}},j=({mensajes:e,children:r})=>{const i=l(a=>a.idioma);return o.jsx(f.I18n,{locale:i,messages:e[i],children:r})};j.propTypes={mensajes:t.object,children:t.element.isRequired};const L=()=>{const e=U();if(!(e!=null&&e.length))return;const r=e[0];return r.indexOf("-")?r.substring(0,r.indexOf("-")):r},k=({mensajes:e,idiomaDefecto:r,children:i})=>{const a=l(c=>c.cambiarIdioma),n=L(),d=I.union(Object.keys(e),Object.keys(p));g.useEffect(()=>{let c=d[0];n&&Object.hasOwn(e,n)?c=n:r&&Object.hasOwn(e,r)&&(c=r),a(c)},[d,e,r,n,a]);const u={};return d.forEach(c=>{u[c]={...Object.hasOwn(e,c)?e[c]:{},saas:Object.hasOwn(p,c)?p[c]:{}}}),o.jsx(j,{mensajes:u,children:i})};k.propTypes={mensajes:t.object,idiomaDefecto:t.string,children:t.element.isRequired};const w=({aplicacion:e,devBackendUrl:r=O,devAuthUrl:i=S,dev:a=!1,redirectUri:n,idiomaDefecto:d="en",mensajes:u={en:{},es:{}},children:c})=>o.jsx(k,{mensajes:u,children:o.jsx(m,{aplicacion:e,devBackendUrl:a&&r,devAuthUrl:a&&i,redirectUri:n,idiomaDefecto:d,children:c})});w.propTypes={aplicacion:t.string.isRequired,devBackendUrl:t.string,devAuthUrl:t.string,dev:t.bool,redirectUri:t.string.isRequired,idiomaDefecto:t.string,mensajes:t.object,children:t.element.isRequired};const _=()=>{const{loginInProgress:e,token:r,tokenData:i,logIn:a,logOut:n,error:d}=g.useContext(v.AuthContext);return{loginInProgress:e,token:r,logIn:a,logOut:n,error:d,correo:i==null?void 0:i.sub,nombre:i==null?void 0:i.name,perfiles:i==null?void 0:i.profiles}};s.Inicializar=m,s.SaasApp=w,s.useAuth=_,s.useSaasStore=l,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@chimera-pe/react-saas",
3
3
  "description": "Componente integrador con SaaS",
4
4
  "author": "Germán Enríquez",
5
- "version": "3.0.0",
5
+ "version": "3.0.1",
6
6
  "type": "module",
7
7
  "repository": {
8
8
  "type": "git",
@@ -26,7 +26,8 @@
26
26
  "dev": "vite",
27
27
  "build": "vite build",
28
28
  "lint": "eslint .",
29
- "preview": "vite preview"
29
+ "preview": "vite preview",
30
+ "prepare": "npm run build"
30
31
  },
31
32
  "peerDependencies": {
32
33
  "lodash": "^4.17.21",