@binamik/js-providers 0.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,20 @@
1
+ interface CookieOptionsType {
2
+ expires?: number;
3
+ path?: string;
4
+ domain?: string;
5
+ secure?: boolean;
6
+ httpOnly?: boolean;
7
+ maxAge?: number;
8
+ sameSite?: 'Strict' | 'Lax' | 'None';
9
+ }
10
+ interface AppStorageProviderProps {
11
+ cookieDomain: string;
12
+ cookiePrefix: string;
13
+ }
14
+ export declare const AppStorageProvider: ({ cookieDomain, cookiePrefix, }: AppStorageProviderProps) => {
15
+ get: (key: string) => string;
16
+ set: (key: string, value: string, options?: CookieOptionsType | undefined) => void;
17
+ remove: (key: string, options?: CookieOptionsType | undefined) => void;
18
+ clean: (options?: CookieOptionsType | undefined) => void;
19
+ };
20
+ export {};
@@ -0,0 +1,25 @@
1
+ export declare class SafeDate {
2
+ private value;
3
+ private isValid;
4
+ get jsDate(): Date;
5
+ get timestamp(): number;
6
+ get isoDate(): string;
7
+ get isoString(): string;
8
+ get dateString(): string;
9
+ get longDayMonth(): string;
10
+ get longDateString(): string;
11
+ get timeString(): string;
12
+ get dateObj(): {
13
+ year: number;
14
+ month: number;
15
+ date: number;
16
+ weekday: number;
17
+ hours: number;
18
+ minutes: number;
19
+ seconds: number;
20
+ };
21
+ addDays(days: number): this;
22
+ addMonths(months: number): this;
23
+ addYears(years: number): this;
24
+ constructor(value?: Date | string);
25
+ }
@@ -0,0 +1,107 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+ import dayjs from "dayjs";
8
+ import "dayjs/locale/pt-br";
9
+ import { parseCookies, setCookie, destroyCookie } from "nookies";
10
+ class SafeDate {
11
+ constructor(value) {
12
+ __publicField(this, "value", dayjs());
13
+ __publicField(this, "isValid", true);
14
+ this.value = dayjs(value).locale("pt-br");
15
+ this.isValid = this.value.isValid();
16
+ if (!this.isValid) {
17
+ console.error("Invalid date:", value);
18
+ }
19
+ }
20
+ get jsDate() {
21
+ return this.value.toDate();
22
+ }
23
+ get timestamp() {
24
+ return this.isValid ? this.jsDate.getTime() : NaN;
25
+ }
26
+ get isoDate() {
27
+ return this.isValid ? this.value.format("YYYY-MM-DD") : "";
28
+ }
29
+ get isoString() {
30
+ return this.isValid ? this.value.format("YYYY-MM-DDTHH:mm:ss") : "";
31
+ }
32
+ get dateString() {
33
+ return this.isValid ? this.value.format("DD/MM/YYYY") : "";
34
+ }
35
+ get longDayMonth() {
36
+ return this.isValid ? this.value.format("DD [de] MMMM") : "";
37
+ }
38
+ get longDateString() {
39
+ return this.isValid ? this.value.format("DD [de] MMMM [de] YYYY") : "";
40
+ }
41
+ get timeString() {
42
+ return this.isValid ? this.value.format("HH:mm") : "";
43
+ }
44
+ get dateObj() {
45
+ return this.isValid ? {
46
+ year: this.value.year(),
47
+ month: this.value.month(),
48
+ date: this.value.date(),
49
+ weekday: this.value.day(),
50
+ hours: this.value.hour(),
51
+ minutes: this.value.minute(),
52
+ seconds: this.value.second()
53
+ } : {
54
+ year: 0,
55
+ month: 0,
56
+ date: 0,
57
+ weekday: 0,
58
+ hours: 0,
59
+ minutes: 0,
60
+ seconds: 0
61
+ };
62
+ }
63
+ addDays(days) {
64
+ this.value = this.isValid ? this.value.add(days, "day") : this.value;
65
+ return this;
66
+ }
67
+ addMonths(months) {
68
+ this.value = this.isValid ? this.value.add(months, "month") : this.value;
69
+ return this;
70
+ }
71
+ addYears(years) {
72
+ this.value = this.isValid ? this.value.add(years, "year") : this.value;
73
+ return this;
74
+ }
75
+ }
76
+ const get = (_domain, prefix) => (key) => {
77
+ const cookies = parseCookies();
78
+ return cookies[`${prefix}-${key}`];
79
+ };
80
+ const set = (domain, prefix) => (key, value, options) => {
81
+ setCookie(null, `${prefix}-${key}`, value, {
82
+ maxAge: (options == null ? void 0 : options.maxAge) || 60 * 60 * 24 * 90,
83
+ path: "/",
84
+ domain,
85
+ secure: true,
86
+ httpOnly: true
87
+ });
88
+ };
89
+ const remove = (_domain, prefix) => (key, options) => {
90
+ destroyCookie(null, `${prefix}-${key}`, options);
91
+ };
92
+ const clean = (domain, prefix) => (options) => {
93
+ remove(domain, prefix)("auth-key", options);
94
+ remove(domain, prefix)("user", options);
95
+ remove(domain, prefix)("company", options);
96
+ remove(domain, prefix)("orb-selected", options);
97
+ };
98
+ const AppStorageProvider = ({
99
+ cookieDomain,
100
+ cookiePrefix
101
+ }) => ({
102
+ get: get(cookieDomain, cookiePrefix),
103
+ set: set(cookieDomain, cookiePrefix),
104
+ remove: remove(cookieDomain, cookiePrefix),
105
+ clean: clean(cookieDomain, cookiePrefix)
106
+ });
107
+ export { AppStorageProvider, SafeDate };
@@ -0,0 +1 @@
1
+ (function(s,a){typeof exports=="object"&&typeof module!="undefined"?a(exports,require("dayjs"),require("dayjs/locale/pt-br"),require("nookies")):typeof define=="function"&&define.amd?define(["exports","dayjs","dayjs/locale/pt-br","nookies"],a):(s=typeof globalThis!="undefined"?globalThis:s||self,a(s.BinamikJSProviders={},s.dayjs,null,s.nookies))})(this,function(s,a,r,l){"use strict";var M=Object.defineProperty;var D=(s,a,r)=>a in s?M(s,a,{enumerable:!0,configurable:!0,writable:!0,value:r}):s[a]=r;var h=(s,a,r)=>(D(s,typeof a!="symbol"?a+"":a,r),r);function v(t){return t&&typeof t=="object"&&"default"in t?t:{default:t}}var o=v(a);class m{constructor(e){h(this,"value",o.default());h(this,"isValid",!0);this.value=o.default(e).locale("pt-br"),this.isValid=this.value.isValid(),this.isValid||console.error("Invalid date:",e)}get jsDate(){return this.value.toDate()}get timestamp(){return this.isValid?this.jsDate.getTime():NaN}get isoDate(){return this.isValid?this.value.format("YYYY-MM-DD"):""}get isoString(){return this.isValid?this.value.format("YYYY-MM-DDTHH:mm:ss"):""}get dateString(){return this.isValid?this.value.format("DD/MM/YYYY"):""}get longDayMonth(){return this.isValid?this.value.format("DD [de] MMMM"):""}get longDateString(){return this.isValid?this.value.format("DD [de] MMMM [de] YYYY"):""}get timeString(){return this.isValid?this.value.format("HH:mm"):""}get dateObj(){return this.isValid?{year:this.value.year(),month:this.value.month(),date:this.value.date(),weekday:this.value.day(),hours:this.value.hour(),minutes:this.value.minute(),seconds:this.value.second()}:{year:0,month:0,date:0,weekday:0,hours:0,minutes:0,seconds:0}}addDays(e){return this.value=this.isValid?this.value.add(e,"day"):this.value,this}addMonths(e){return this.value=this.isValid?this.value.add(e,"month"):this.value,this}addYears(e){return this.value=this.isValid?this.value.add(e,"year"):this.value,this}}const y=(t,e)=>i=>l.parseCookies()[`${e}-${i}`],c=(t,e)=>(i,d,n)=>{l.setCookie(null,`${e}-${i}`,d,{maxAge:(n==null?void 0:n.maxAge)||60*60*24*90,path:"/",domain:t,secure:!0,httpOnly:!0})},u=(t,e)=>(i,d)=>{l.destroyCookie(null,`${e}-${i}`,d)},f=(t,e)=>i=>{u(t,e)("auth-key",i),u(t,e)("user",i),u(t,e)("company",i),u(t,e)("orb-selected",i)},g=({cookieDomain:t,cookiePrefix:e})=>({get:y(t,e),set:c(t,e),remove:u(t,e),clean:f(t,e)});s.AppStorageProvider=g,s.SafeDate=m,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -0,0 +1,3 @@
1
+ export { SafeDate } from './SafeDate/SafeDate';
2
+ export type { SafeDate as SafeDateType } from './SafeDate/SafeDate';
3
+ export { AppStorageProvider } from './AppStorageProvider/AppStorageProvider';
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@binamik/js-providers",
3
+ "version": "0.0.2",
4
+ "scripts": {
5
+ "dev": "vite",
6
+ "build": "tsc && vite build",
7
+ "test": "vitest"
8
+ },
9
+ "devDependencies": {
10
+ "@types/node": "^17.0.41",
11
+ "axios": "^0.27.2",
12
+ "dayjs": "^1.11.3",
13
+ "nookies": "^2.5.2",
14
+ "typescript": "^4.5.4",
15
+ "vite": "^2.9.9",
16
+ "vite-plugin-dts": "^1.2.0",
17
+ "vitest": "^0.16.0",
18
+ "yup": "^0.32.11"
19
+ },
20
+ "peerDependencies": {
21
+ "axios": ">=0.20.0",
22
+ "dayjs": ">=1.11.0",
23
+ "nookies": ">=2.0.0",
24
+ "yup": ">=0.30.0"
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "main": "./dist/binamik-providers.umd.js",
30
+ "module": "./dist/binamik-providers.es.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "import": "./dist/binamik-providers.es.js",
35
+ "require": "./dist/binamik-providers.umd.js"
36
+ }
37
+ }
38
+ }