@econneq/gql-auth 1.0.1 → 1.0.4

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,31 @@
1
+ type SubmitOptions = {
2
+ newData: any;
3
+ editData?: any;
4
+ mutationName: string;
5
+ modelName: string;
6
+ successField?: string;
7
+ query: any;
8
+ params: any;
9
+ router: any;
10
+ token?: string;
11
+ config: {
12
+ protocol: string;
13
+ RootApi: string;
14
+ NoDomainRootApi: string;
15
+ Subdomains: any[];
16
+ };
17
+ onAlert: (options: {
18
+ title: string;
19
+ status: boolean;
20
+ duration: number;
21
+ }) => void;
22
+ redirect?: boolean;
23
+ redirectPath?: string;
24
+ returnResponseField?: boolean;
25
+ returnResponseObject?: boolean;
26
+ reload?: boolean;
27
+ getFileMap?: (item: any) => Record<string, File>;
28
+ };
29
+ export declare const ApiFactory: (options: SubmitOptions) => Promise<any>;
30
+ export {};
31
+ //# sourceMappingURL=api-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-factory.d.ts","sourceRoot":"","sources":["../src/api-factory.ts"],"names":[],"mappings":"AAIA,KAAK,aAAa,GAAG;IACnB,OAAO,EAAE,GAAG,CAAC;IACb,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,GAAG,CAAC;IACX,MAAM,EAAE,GAAG,CAAC;IACZ,MAAM,EAAE,GAAG,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE;QACN,QAAQ,EAAE,MAAM,CAAC;QACjB,OAAO,EAAE,MAAM,CAAC;QAChB,eAAe,EAAE,MAAM,CAAC;QACxB,UAAU,EAAE,GAAG,EAAE,CAAC;KACnB,CAAC;IACF,OAAO,EAAE,CAAC,OAAO,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,OAAO,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IACjF,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;CAClD,CAAC;AAEF,eAAO,MAAM,UAAU,GAAU,SAAS,aAAa,iBA4DtD,CAAC"}
@@ -0,0 +1,68 @@
1
+ import { uploadGraphQLMutation } from "./upload-gql";
2
+ import { removeEmptyFields } from "./utils";
3
+ export const ApiFactory = async (options) => {
4
+ const { newData, editData, mutationName, modelName, successField = "title", query, router, params, reload = true, redirect, redirectPath, returnResponseField = false, returnResponseObject = false, getFileMap, token, config, onAlert } = options;
5
+ const items = Array.isArray(newData || editData) ? (newData || editData) : [(newData || editData)];
6
+ const successMessages = [];
7
+ const errorMessages = [];
8
+ let responseFieldData = null;
9
+ for (let res of items) {
10
+ try {
11
+ console.log("launching e-conneq/auth-gql ......");
12
+ const response = await uploadGraphQLMutation({
13
+ query: query.loc?.source.body || "",
14
+ variables: removeEmptyFields(res),
15
+ fileMap: getFileMap ? getFileMap(res) : {},
16
+ params, // Pass domain params for multi-tenancy
17
+ token,
18
+ config, // Pass global URLs and Subdomain list
19
+ });
20
+ console.log("response e-conneq/auth-gql ......");
21
+ const result = response?.data?.[mutationName]?.[modelName];
22
+ if (response?.data?.[mutationName]) {
23
+ successMessages.push(result?.[successField] || "operation successful ✅");
24
+ if (returnResponseObject)
25
+ responseFieldData = response.data[mutationName];
26
+ if (result?.id && returnResponseField)
27
+ responseFieldData = result[successField];
28
+ if (result?.id || result?.token || result === null) {
29
+ }
30
+ }
31
+ // Error Handling
32
+ if (response?.errors) {
33
+ handleGraphQLErrors(response.errors, errorMessages);
34
+ }
35
+ }
36
+ catch (err) {
37
+ errorMessages.push(`Error: ${err.message}`);
38
+ }
39
+ }
40
+ // Handle Response/UI Logic
41
+ if (successMessages.length > 0 && errorMessages.length === 0) {
42
+ if (returnResponseObject || returnResponseField)
43
+ return responseFieldData;
44
+ onAlert({ title: "successfully submitted ✅", status: true, duration: 3000 });
45
+ if (redirect && redirectPath)
46
+ router.push(redirectPath);
47
+ else if (reload)
48
+ window.location.reload();
49
+ }
50
+ else if (errorMessages.length > 0) {
51
+ onAlert({
52
+ title: `❌ Errors:\n${errorMessages.join("\n")}`,
53
+ status: false,
54
+ duration: 5000
55
+ });
56
+ }
57
+ };
58
+ // Internal Helper for Error Parsing
59
+ const handleGraphQLErrors = (errors, errorList) => {
60
+ errors.forEach(error => {
61
+ if (error.message.includes("duplicate"))
62
+ errorList.push("Record Already Exists ❌");
63
+ else if (error.message.includes("Authentication"))
64
+ errorList.push("Login Required ❌");
65
+ else
66
+ errorList.push(error.message);
67
+ });
68
+ };
@@ -0,0 +1,12 @@
1
+ import { ApolloClient } from '@apollo/client';
2
+ export default function getApolloClient(options?: {
3
+ csrfToken?: string;
4
+ cookie?: string;
5
+ }, domain?: string | null, config?: {
6
+ protocol: string;
7
+ RootApi: string;
8
+ NoDomainRootApi: string;
9
+ Subdomains: any[];
10
+ apiKey: string;
11
+ }): Promise<ApolloClient | null>;
12
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAA2B,MAAM,gBAAgB,CAAC;AAGvE,wBAA8B,eAAe,CAC3C,OAAO,CAAC,EAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,EACjD,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,EACtB,MAAM,CAAC,EAAE;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,UAAU,EAAE,GAAG,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB,GACA,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CA+C9B"}
package/dist/client.js ADDED
@@ -0,0 +1,45 @@
1
+ // src/client.ts
2
+ import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
3
+ import Cookies from "js-cookie";
4
+ export default async function getApolloClient(options, domain, config) {
5
+ if (!config)
6
+ throw new Error("Configuration is required for getApolloClient");
7
+ const isServer = typeof window === "undefined";
8
+ const { protocol, RootApi, NoDomainRootApi, Subdomains, apiKey } = config;
9
+ // 1. Find Tenant Data
10
+ const tenant = domain
11
+ ? Subdomains.find((s) => s.subdomain.toLowerCase() === domain.toLowerCase())
12
+ : null;
13
+ // 2. Multi-tenant URL Logic
14
+ // Fallback to NoDomainRootApi if no valid tenant is found
15
+ const baseUrl = tenant
16
+ ? `${protocol}api${tenant.subdomain}${RootApi}`
17
+ : `${protocol}${NoDomainRootApi}`;
18
+ const uri = `${baseUrl}/graphql/`;
19
+ // 3. Header Logic
20
+ const csrfToken = options?.csrfToken || (!isServer ? Cookies.get("csrftoken") : "");
21
+ const headers = {
22
+ 'Content-Type': 'application/json',
23
+ 'X-API-KEY': apiKey,
24
+ ...(options?.cookie && { 'Cookie': options.cookie }),
25
+ ...(csrfToken && { 'X-CSRFToken': csrfToken }),
26
+ ...(tenant && {
27
+ 'X-Tenant-ID': tenant.id.toString(),
28
+ 'X-Process-ID': tenant.processId,
29
+ 'X-Widget-ID': tenant.widgetID || "",
30
+ }),
31
+ 'Referer': baseUrl,
32
+ };
33
+ return new ApolloClient({
34
+ link: new HttpLink({
35
+ uri,
36
+ fetch, // Next.js polyfills fetch on server automatically
37
+ headers,
38
+ }),
39
+ cache: new InMemoryCache(),
40
+ defaultOptions: {
41
+ mutate: { fetchPolicy: 'no-cache' },
42
+ query: { fetchPolicy: 'no-cache' },
43
+ },
44
+ });
45
+ }
@@ -0,0 +1,82 @@
1
+ import { ReactNode } from "react";
2
+ export { queryServerGraphQL } from './query-server';
3
+ export { ApiFactory } from './api-factory';
4
+ export { uploadGraphQLMutation } from './upload-gql';
5
+ export type ConfigType = {
6
+ protocol: string;
7
+ RootApi: string;
8
+ NoDomainRootApi: string;
9
+ Subdomains: SubdomainItem[];
10
+ apiKey: string;
11
+ fonts: string[];
12
+ };
13
+ export type SubdomainItem = {
14
+ id: number;
15
+ subdomain: string;
16
+ processId: string;
17
+ widgetID?: string;
18
+ };
19
+ export type ApolloOptions = {
20
+ csrfToken?: string;
21
+ cookie?: string;
22
+ };
23
+ export type SelectOptions = string | {
24
+ label: string;
25
+ value: string | number;
26
+ };
27
+ export interface InterLoginData {
28
+ username: string;
29
+ password: string;
30
+ }
31
+ export interface JwtPayload {
32
+ iss?: string;
33
+ sub?: string;
34
+ aud?: string[] | string;
35
+ exp: number;
36
+ nbf?: number;
37
+ iat?: number;
38
+ jti?: string;
39
+ user_id?: number;
40
+ username?: string;
41
+ matricle?: string;
42
+ photo?: string;
43
+ is_superuser?: boolean;
44
+ is_staff?: boolean;
45
+ is_active?: boolean;
46
+ is_hod?: boolean;
47
+ role?: string;
48
+ dept?: string[] | any;
49
+ page?: string[] | any;
50
+ school?: number[] | any;
51
+ domain?: number[] | any;
52
+ language?: string[];
53
+ last_login?: any;
54
+ }
55
+ export interface InterField {
56
+ name: string;
57
+ label: string;
58
+ inputType: 'text' | "textarea" | 'number' | 'float' | 'search-select' | 'select' | 'file' | 'date' | 'password';
59
+ type: 'text' | 'textarea' | 'number' | 'float' | 'email' | 'date' | 'file' | 'password';
60
+ required?: boolean;
61
+ min?: number;
62
+ max?: number;
63
+ message?: string;
64
+ options?: {
65
+ value: string | number;
66
+ label: string;
67
+ }[];
68
+ show?: boolean;
69
+ sort?: boolean;
70
+ placeholder?: string;
71
+ acceptedFileType?: string;
72
+ icon?: ReactNode;
73
+ labelIcon?: ReactNode;
74
+ }
75
+ export interface InterFieldList {
76
+ rowId: number | string;
77
+ desktopCols: 1 | 2 | 3 | 4;
78
+ mobileCols: 1 | 2;
79
+ fields: InterField[];
80
+ }
81
+ export { capitalizeEachWord, removeEmptyFields, errorLog, decodeUrlID, formatText, getUser, getToken, getTemplate, getLanguage, getStoredFont, getTimeAgo, Alert, validateFormFields, } from './utils';
82
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ // 1. Export the Core Functions
2
+ export { queryServerGraphQL } from './query-server';
3
+ export { ApiFactory } from './api-factory';
4
+ export { uploadGraphQLMutation } from './upload-gql';
5
+ // 3. Export Helper Utilities (Optional)
6
+ export { capitalizeEachWord, removeEmptyFields, errorLog, decodeUrlID, formatText, getUser, getToken, getTemplate, getLanguage, getStoredFont, getTimeAgo, Alert, validateFormFields, } from './utils';
@@ -0,0 +1,15 @@
1
+ type ServerQueryArgs = {
2
+ query: any;
3
+ variables?: Record<string, any>;
4
+ domain?: string | null;
5
+ config: {
6
+ protocol: string;
7
+ RootApi: string;
8
+ NoDomainRootApi: string;
9
+ Subdomains: any[];
10
+ apiKey: string;
11
+ };
12
+ };
13
+ export declare function queryServerGraphQL<T = any>({ query, variables, domain, config, }: ServerQueryArgs): Promise<T | null>;
14
+ export {};
15
+ //# sourceMappingURL=query-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-server.d.ts","sourceRoot":"","sources":["../src/query-server.ts"],"names":[],"mappings":"AAKA,KAAK,eAAe,GAAG;IACrB,KAAK,EAAE,GAAG,CAAC;IACX,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,MAAM,EAAE;QACN,QAAQ,EAAE,MAAM,CAAC;QACjB,OAAO,EAAE,MAAM,CAAC;QAChB,eAAe,EAAE,MAAM,CAAC;QACxB,UAAU,EAAE,GAAG,EAAE,CAAC;QAClB,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH,CAAC;AAEF,wBAAsB,kBAAkB,CAAC,CAAC,GAAG,GAAG,EAAE,EAChD,KAAK,EACL,SAAc,EACd,MAAM,EACN,MAAM,GACP,EAAE,eAAe,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CA8BrC"}
@@ -0,0 +1,30 @@
1
+ // src/server-query.ts
2
+ 'use server';
3
+ import { cookies } from 'next/headers';
4
+ import getApolloClient from './client';
5
+ export async function queryServerGraphQL({ query, variables = {}, domain, config, }) {
6
+ try {
7
+ // 1. Get server-side cookies
8
+ const cookieStore = await cookies();
9
+ const cookieString = cookieStore.toString();
10
+ // 2. Call getApolloClient with the correct 3 arguments:
11
+ // Arg 1: options (cookies)
12
+ // Arg 2: domain (subdomain string)
13
+ // Arg 3: config (URLs and Subdomain array)
14
+ const client = await getApolloClient({ cookie: cookieString }, domain, config);
15
+ if (!client)
16
+ return null;
17
+ const result = await client.query({
18
+ query,
19
+ variables,
20
+ fetchPolicy: 'no-cache',
21
+ });
22
+ console.log("getting @e-conneq/gql-auth ........");
23
+ return result?.data;
24
+ }
25
+ catch (err) {
26
+ // On the server, we console.error instead of using Swal
27
+ console.error("GraphQL Server Error:", err);
28
+ return null;
29
+ }
30
+ }
@@ -0,0 +1,17 @@
1
+ type UploadGraphQLArgs = {
2
+ query: string;
3
+ variables: Record<string, any>;
4
+ fileMap: Record<string, File>;
5
+ token?: string;
6
+ params: any;
7
+ config: {
8
+ protocol: string;
9
+ RootApi: string;
10
+ NoDomainRootApi: string;
11
+ Subdomains: any[];
12
+ apiKey?: string;
13
+ };
14
+ };
15
+ export declare function uploadGraphQLMutation({ query, variables, fileMap, token, params, config }: UploadGraphQLArgs): Promise<any>;
16
+ export {};
17
+ //# sourceMappingURL=upload-gql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload-gql.d.ts","sourceRoot":"","sources":["../src/upload-gql.ts"],"names":[],"mappings":"AAAA,KAAK,iBAAiB,GAAG;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC/B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,GAAG,CAAC;IACZ,MAAM,EAAE;QACN,QAAQ,EAAE,MAAM,CAAC;QACjB,OAAO,EAAE,MAAM,CAAC;QAChB,eAAe,EAAE,MAAM,CAAC;QACxB,UAAU,EAAE,GAAG,EAAE,CAAC;QAClB,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,CAAC;CACH,CAAC;AAEF,wBAAsB,qBAAqB,CAAC,EAC1C,KAAK,EACL,SAAS,EACT,OAAO,EACP,KAAK,EACL,MAAM,EACN,MAAM,EACP,EAAE,iBAAiB,gBA4DnB"}
@@ -0,0 +1,47 @@
1
+ export async function uploadGraphQLMutation({ query, variables, fileMap, token, params, config }) {
2
+ const { protocol, RootApi, NoDomainRootApi, Subdomains, apiKey } = config;
3
+ const formData = new FormData();
4
+ const updatedVariables = { ...variables };
5
+ // 1. Prepare GraphQL Multipart Request (Spec compliant)
6
+ Object.keys(fileMap).forEach((key) => {
7
+ updatedVariables[key] = null;
8
+ });
9
+ const operations = { query, variables: updatedVariables };
10
+ const map = {};
11
+ const fileKeys = Object.keys(fileMap);
12
+ fileKeys.forEach((key, index) => {
13
+ map[`${index}`] = [`variables.${key}`];
14
+ });
15
+ formData.append("operations", JSON.stringify(operations));
16
+ formData.append("map", JSON.stringify(map));
17
+ fileKeys.forEach((key, index) => {
18
+ formData.append(`${index}`, fileMap[key]);
19
+ });
20
+ // 2. Multi-tenant URL and Data Logic
21
+ const requestedDomain = params?.domain || "";
22
+ const tenantData = Subdomains.find((d) => d.subdomain.toLowerCase() === requestedDomain.toLowerCase());
23
+ // Determine URL: Use subdomain api if tenant exists, otherwise use root API
24
+ const API_LINK = tenantData
25
+ ? `${protocol}api${tenantData.subdomain}${RootApi}/graphql/`
26
+ : `${protocol}${NoDomainRootApi}/graphql/`;
27
+ // 3. Construct Headers
28
+ const headers = {
29
+ ...(token ? { "Authorization": `Bearer ${token}` } : {}),
30
+ ...(apiKey ? { "X-API-KEY": apiKey } : {}),
31
+ };
32
+ // Inject specific tenant metadata into headers for Django to read
33
+ if (tenantData) {
34
+ headers["X-Tenant-ID"] = String(tenantData.id);
35
+ headers["X-Process-ID"] = tenantData.processId || "";
36
+ headers["X-Widget-ID"] = tenantData.widgetID || "";
37
+ }
38
+ // 4. Execute Fetch
39
+ const res = await fetch(API_LINK, {
40
+ method: "POST",
41
+ headers,
42
+ body: formData,
43
+ // credentials: "include" is important if you use cookies alongside headers
44
+ credentials: "include",
45
+ });
46
+ return await res.json();
47
+ }
@@ -0,0 +1,24 @@
1
+ import { InterFieldList, JwtPayload } from ".";
2
+ export declare const capitalizeEachWord: (str: string) => string;
3
+ export declare const errorLog: (err: any, show?: boolean) => string;
4
+ export declare const getToken: () => string | null;
5
+ export declare const getUser: () => JwtPayload | null;
6
+ export declare const getStoredFont: (FONTS: string[]) => string;
7
+ export declare const getTimeAgo: (dateStr: string, type: "past" | "future") => string;
8
+ export declare const getLanguage: () => "en" | "fr";
9
+ export declare const getTemplate: () => number;
10
+ export declare const decodeUrlID: (urlID: string) => string;
11
+ export declare function getAcademicYear(): string;
12
+ export declare const removeEmptyFields: (obj: any) => any;
13
+ export declare const Alert: ({ title, duration, status }: {
14
+ title: string;
15
+ duration: 1000 | 1500 | 2000 | 3000 | 4000 | 5000;
16
+ status?: boolean;
17
+ }) => void;
18
+ export declare const validateFormFields: (formData: any, currentStepFields: InterFieldList[]) => Record<string, string>;
19
+ export declare const lastYears: (number: number) => {
20
+ value: string;
21
+ label: string;
22
+ }[];
23
+ export declare const formatText: (text: string) => string;
24
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,GAAG,CAAC;AAI/C,eAAO,MAAM,kBAAkB,GAAI,KAAK,MAAM,WAM7C,CAAC;AAEF,eAAO,MAAM,QAAQ,GAAI,KAAK,GAAG,EAAE,OAAO,OAAO,WAyBhD,CAAC;AAKF,eAAO,MAAM,QAAQ,qBAKpB,CAAC;AAEF,eAAO,MAAM,OAAO,yBAKnB,CAAA;AAED,eAAO,MAAM,aAAa,GAAI,OAAO,MAAM,EAAE,WAO5C,CAAC;AAEF,eAAO,MAAM,UAAU,GACnB,SAAS,MAAM,EACf,MAAM,MAAM,GAAG,QAAQ,KACxB,MA0CF,CAAC;AAGF,eAAO,MAAM,WAAW,mBAOvB,CAAC;AAEF,eAAO,MAAM,WAAW,cAMvB,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,OAAO,MAAM,WAIxC,CAAA;AAED,wBAAgB,eAAe,IAAI,MAAM,CAUxC;AAED,eAAO,MAAM,iBAAiB,GAAI,KAAK,GAAG,QAUzC,CAAC;AAGF,eAAO,MAAM,KAAK,GACd,6BACI;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,SAU7F,CAAC;AAIF,eAAO,MAAM,kBAAkB,GAAI,UAAU,GAAG,EAAE,mBAAmB,cAAc,EAAE,2BAkDpF,CAAC;AAEF,eAAO,MAAM,SAAS,GAAI,QAAQ,MAAM;;;GAKvC,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,WAiDtC,CAAC"}
package/dist/utils.js ADDED
@@ -0,0 +1,257 @@
1
+ import Swal from "sweetalert2";
2
+ import Cookies from "js-cookie";
3
+ import { jwtDecode } from "jwt-decode";
4
+ export const capitalizeEachWord = (str) => {
5
+ if (!str)
6
+ return ''; // Handle empty or null strings
7
+ return str
8
+ .split(' ')
9
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
10
+ .join(' ');
11
+ };
12
+ export const errorLog = (err, show) => {
13
+ let mes = "An unknown error occurred";
14
+ if (typeof err === "string")
15
+ mes = err;
16
+ else if (err?.graphQLErrors?.length > 0)
17
+ mes = err.graphQLErrors.map((e) => e.message).join('\n');
18
+ else if (err?.networkError) {
19
+ const netErr = err.networkError;
20
+ if ("result" in netErr && netErr.result?.errors?.length > 0)
21
+ mes = netErr.result.errors.map((e) => e.message).join('\n');
22
+ else if (netErr.message)
23
+ mes = netErr.message;
24
+ }
25
+ else if (err?.extraInfo)
26
+ mes = String(err.extraInfo);
27
+ else if (err?.message)
28
+ mes = err.message;
29
+ if (show) {
30
+ Swal.fire({
31
+ title: mes,
32
+ icon: 'error',
33
+ timer: 3000,
34
+ timerProgressBar: true,
35
+ showConfirmButton: false,
36
+ });
37
+ }
38
+ return mes;
39
+ };
40
+ const currentYear = new Date().getFullYear();
41
+ export const getToken = () => {
42
+ if (typeof window === "undefined") {
43
+ return null;
44
+ }
45
+ return Cookies.get("token") || localStorage.getItem("token");
46
+ };
47
+ export const getUser = () => {
48
+ // const token = typeof window !== 'undefined' ? localStorage.getItem("token") : null;
49
+ const token = getToken();
50
+ const user = token ? jwtDecode(token) : null;
51
+ return user;
52
+ };
53
+ export const getStoredFont = (FONTS) => {
54
+ if (typeof window === "undefined")
55
+ return FONTS[0]; // Default for SSR
56
+ const up = localStorage.getItem("user-pref");
57
+ if (!up)
58
+ return FONTS[5]; // Default to Ubuntu (6th index)
59
+ const fontIndex = parseInt(up[1]) - 1;
60
+ return FONTS[fontIndex] || FONTS[5];
61
+ };
62
+ export const getTimeAgo = (dateStr, type) => {
63
+ if (!dateStr)
64
+ return "";
65
+ const now = new Date();
66
+ const item = new Date(dateStr);
67
+ let diffMs = 0;
68
+ if (type === "past")
69
+ diffMs = now.getTime() - item.getTime();
70
+ if (type === "future")
71
+ diffMs = item.getTime() - now.getTime();
72
+ // Handle invalid date ranges
73
+ if (diffMs < 0) {
74
+ return type === "past" ? "just now" : "Closed";
75
+ }
76
+ const seconds = Math.floor(diffMs / 1000);
77
+ const minutes = Math.floor(seconds / 60);
78
+ const hours = Math.floor(minutes / 60);
79
+ const days = Math.floor(hours / 24);
80
+ const weeks = Math.floor(days / 7);
81
+ const months = Math.floor(days / 30.44); // Use average month length
82
+ const years = Math.floor(days / 365.25); // Account for leap years
83
+ const suffix = type === "past" ? "ago" : "left";
84
+ // 1. Logic for Past Dates (e.g., Posted 2 days ago)
85
+ if (type === "past") {
86
+ if (days === 0)
87
+ return "Today";
88
+ if (days === 1)
89
+ return "Yesterday";
90
+ if (days < 7)
91
+ return `${days} days ago`;
92
+ if (weeks < 5)
93
+ return `${weeks} week${weeks > 1 ? "s" : ""} ago`;
94
+ if (months < 12)
95
+ return `${months} month${months > 1 ? "s" : ""} ago`;
96
+ return `${years} year${years > 1 ? "s" : ""} ago`;
97
+ }
98
+ // 2. Logic for Future Dates (e.g., 5 days left)
99
+ if (days === 0)
100
+ return "Ends today";
101
+ if (days === 1)
102
+ return "1 day left";
103
+ if (days < 7)
104
+ return `${days} days left`;
105
+ if (weeks < 5)
106
+ return `${weeks} week${weeks > 1 ? "s" : ""} left`;
107
+ if (months < 12)
108
+ return `${months} month${months > 1 ? "s" : ""} left`;
109
+ return `${years} year${years > 1 ? "s" : ""} left`;
110
+ };
111
+ export const getLanguage = () => {
112
+ if (typeof window === "undefined")
113
+ return "en";
114
+ const up = localStorage.getItem("user-pref");
115
+ if (!up)
116
+ return "en";
117
+ const langNumber = parseInt(up[0]);
118
+ return langNumber === 1 ? "en" : "fr";
119
+ };
120
+ export const getTemplate = () => {
121
+ if (typeof window === "undefined")
122
+ return 1;
123
+ const up = localStorage.getItem("user-pref");
124
+ if (!up)
125
+ return 1;
126
+ return parseInt(up[2]);
127
+ };
128
+ export const decodeUrlID = (urlID) => {
129
+ const base64DecodedString = decodeURIComponent(urlID); // Decodes %3D%3D to ==
130
+ const id = Buffer.from(base64DecodedString, 'base64').toString('utf-8'); // Decoding from base64
131
+ return id.split(":")[1];
132
+ };
133
+ export function getAcademicYear() {
134
+ const today = new Date();
135
+ const year = today.getFullYear();
136
+ const month = today.getMonth(); // 0 = January, 7 = August
137
+ if (month < 7) {
138
+ return `${year - 1}/${year}`;
139
+ }
140
+ else {
141
+ return `${year}/${year + 1}`;
142
+ }
143
+ }
144
+ export const removeEmptyFields = (obj) => {
145
+ const newObj = {};
146
+ for (const key in obj) {
147
+ // Keep File objects and non-empty values
148
+ if (obj[key] instanceof File ||
149
+ (obj[key] !== null && obj[key] !== undefined && obj[key] !== '')) {
150
+ newObj[key] = obj[key];
151
+ }
152
+ }
153
+ return newObj;
154
+ };
155
+ export const Alert = ({ title, duration, status = true }) => {
156
+ Swal.fire({
157
+ title: capitalizeEachWord(title),
158
+ timer: duration,
159
+ timerProgressBar: true,
160
+ showConfirmButton: false,
161
+ icon: status ? 'success' : 'error',
162
+ });
163
+ };
164
+ export const validateFormFields = (formData, currentStepFields) => {
165
+ const newErrors = {};
166
+ // 1. Flatten the fields from all rows in the step
167
+ const fieldsToValidate = currentStepFields.flatMap(row => row.fields);
168
+ fieldsToValidate.forEach((field) => {
169
+ // Skip validation if the field is hidden
170
+ if (field.show === false)
171
+ return;
172
+ // 2. Get the value from formData
173
+ const value = formData[field.name];
174
+ // 3. Check Required
175
+ if (field.required && (!value || value.toString().trim() === "")) {
176
+ newErrors[field.name] = `${capitalizeEachWord(field.label)} is required`;
177
+ // return;
178
+ }
179
+ // 4. Check Type (Email)
180
+ if (field.type === 'email' && value) {
181
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
182
+ if (!emailRegex.test(value)) {
183
+ newErrors[field.name] = "Invalid email format";
184
+ }
185
+ }
186
+ // 5. Check Numbers (Min/Max)
187
+ if (field.type === 'number' && value) {
188
+ const numValue = Number(value);
189
+ if (field.min !== undefined && numValue < field.min) {
190
+ newErrors[field.name] = `Minimum value is ${field.min}`;
191
+ }
192
+ if (field.max !== undefined && numValue > field.max) {
193
+ newErrors[field.name] = `Maximum value is ${field.max}`;
194
+ }
195
+ }
196
+ // 6. Check Length (Strings)
197
+ if (field.type === 'text' && value) {
198
+ if (field.min !== undefined && value.length < field.min) {
199
+ newErrors[field.name] = `Too short (min ${field.min} chars)`;
200
+ }
201
+ if (field.max !== undefined && value.length > field.max) {
202
+ newErrors[field.name] = `Too long (min ${field.max} chars)`;
203
+ }
204
+ }
205
+ });
206
+ return newErrors;
207
+ };
208
+ export const lastYears = (number) => {
209
+ return Array.from({ length: number }, (_, i) => ({
210
+ value: (currentYear - i).toString(),
211
+ label: (currentYear - i).toString()
212
+ })).sort((a, b) => Number(b.value) - Number(a.value));
213
+ };
214
+ export const formatText = (text) => {
215
+ return text;
216
+ if (!text || typeof text !== "string")
217
+ return "";
218
+ return text
219
+ // Ensure space after "." if it's not part of abbreviation/number
220
+ .replace(/\.(?!\s|$|[a-zA-Z0-9])/g, ". ")
221
+ // Split sentences by ". " but preserve delimiter
222
+ .split(/(\. )/g)
223
+ .map((segment) => {
224
+ if (segment === ". ")
225
+ return segment; // keep delimiter
226
+ return segment
227
+ .split(" ")
228
+ .map((word, i) => {
229
+ if (word === "")
230
+ return word;
231
+ // Preserve existing ALL CAPS words
232
+ if (word.length > 1 && /^[A-Z]+$/.test(word)) {
233
+ return word;
234
+ }
235
+ // Abbreviations like g.a, n.l.n
236
+ if (/^([a-zA-Z]\.)+[a-zA-Z]?$/.test(word)) {
237
+ return word.toUpperCase(); // → G.A, N.L.N
238
+ }
239
+ // Version numbers like 3.0.1
240
+ if (/^\d+(\.\d+)+$/.test(word)) {
241
+ return word;
242
+ }
243
+ // Mixed abbrev/numbers (like p.m2.5 or covid-19 v2.0)
244
+ if (/[a-zA-Z]\.\d/.test(word) || /\d+\.\d+[a-zA-Z]?/.test(word)) {
245
+ return word; // leave as-is
246
+ }
247
+ // First word of sentence → capitalize first letter
248
+ if (i === 0) {
249
+ return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
250
+ }
251
+ // Other words → lowercase
252
+ return word.toLowerCase();
253
+ })
254
+ .join(" ");
255
+ })
256
+ .join("");
257
+ };
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "@econneq/gql-auth",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "main": "./dist/index.js",
5
- "module": "./dist/index.mjs",
5
+ "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "prepublishOnly": "npm run build"
10
+ },
7
11
  "exports": {
8
12
  ".": {
9
13
  "types": "./dist/index.d.ts",
10
- "import": "./dist/index.mjs",
14
+ "import": "./dist/index.js",
11
15
  "require": "./dist/index.js"
12
16
  }
13
17
  },