@chimera-pe/react-saas 3.0.0 → 3.0.2

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