@matheusdev20/hc-lib 0.1.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.
package/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # @matheusdev20/hc-lib
2
+
3
+ Utility library for Zendesk Help Center development. Supports both **v3** and **v4** Help Center versions through a single factory method.
4
+
5
+ ## CDN Usage
6
+
7
+ Add the script tag to your Help Center template:
8
+
9
+ ```html
10
+ <!-- jsDelivr -->
11
+ <script src="https://cdn.jsdelivr.net/npm/@matheusdev20/hc-lib/dist/index.global.js"></script>
12
+
13
+ <!-- unpkg -->
14
+ <script src="https://unpkg.com/@matheusdev20/hc-lib/dist/index.global.js"></script>
15
+ ```
16
+
17
+ To pin a specific version, replace `hc-lib` with `hc-lib@1.0.0`.
18
+
19
+ This exposes the global `AktieHC` object.
20
+
21
+ ## Instantiating
22
+
23
+ Call `AktieHC.create()` with the Help Center version:
24
+
25
+ ```html
26
+ <script>
27
+ // For Help Center v3
28
+ var hc = AktieHC.create("v3");
29
+
30
+ // For Help Center v4
31
+ var hc = AktieHC.create("v4");
32
+ </script>
33
+ ```
34
+
35
+ The returned `hc` object has the same interface regardless of version — the implementation details are handled internally.
36
+
37
+ ## Available Methods
38
+
39
+ ```js
40
+ var hc = AktieHC.create("v3");
41
+
42
+ hc.hideFormField("field_id");
43
+ hc.hideCustomFieldOption("field_id", "option_value");
44
+ hc.hideAttachmentField();
45
+ ```
46
+
47
+ ## Waiting for DOM Ready
48
+
49
+ Use `AktieHC.ready()` to ensure the DOM is loaded before manipulating elements:
50
+
51
+ ```html
52
+ <script>
53
+ AktieHC.ready(function () {
54
+ var hc = AktieHC.create("v4");
55
+ hc.hideFormField("field_id");
56
+ });
57
+ </script>
58
+ ```
@@ -0,0 +1,15 @@
1
+ declare function ready(fn: () => void): void;
2
+
3
+ declare function noop(): void;
4
+ declare function ping(): string;
5
+ declare function timeout(ms: number): Promise<never>;
6
+
7
+ type HCVersion = "v3" | "v4";
8
+ interface HCInstance {
9
+ getAllArticles(): Promise<any>;
10
+ }
11
+ type APIDependencies = 'articles';
12
+
13
+ declare function create(version: HCVersion, apiDependencies?: APIDependencies[]): HCInstance;
14
+
15
+ export { type HCInstance, type HCVersion, create, noop, ping, ready, timeout };
@@ -0,0 +1,15 @@
1
+ declare function ready(fn: () => void): void;
2
+
3
+ declare function noop(): void;
4
+ declare function ping(): string;
5
+ declare function timeout(ms: number): Promise<never>;
6
+
7
+ type HCVersion = "v3" | "v4";
8
+ interface HCInstance {
9
+ getAllArticles(): Promise<any>;
10
+ }
11
+ type APIDependencies = 'articles';
12
+
13
+ declare function create(version: HCVersion, apiDependencies?: APIDependencies[]): HCInstance;
14
+
15
+ export { type HCInstance, type HCVersion, create, noop, ping, ready, timeout };
@@ -0,0 +1,2 @@
1
+ "use strict";var AktieHC=(()=>{var l=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var h=(t,e)=>{for(var n in e)l(t,n,{get:e[n],enumerable:!0})},w=(t,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of g(e))!y.call(t,r)&&r!==n&&l(t,r,{get:()=>e[r],enumerable:!(s=f(e,r))||s.enumerable});return t};var x=t=>w(l({},"__esModule",{value:!0}),t);var C={};h(C,{create:()=>m,noop:()=>I,ping:()=>P,ready:()=>A,timeout:()=>v});function A(t){document.readyState!=="loading"?t():document.addEventListener("DOMContentLoaded",t)}function I(){}function P(){let t="hc-lib loaded successfully!";return console.log(t),t}function v(t){return new Promise((e,n)=>setTimeout(()=>n(new Error("Request timeout")),t))}var i=class{constructor(e){this.api=e!=null?e:{}}use(e){let n=this.api[e];if(!n)throw new Error(`HCv4: "${e}" was not provided as a dependency. Pass it when creating the instance: create("v4", ["${e}"])`);return n}getAllArticles(){return this.use("articles").getAll()}};var d={web:{subdomain:window.location.host,locale:window.location.pathname.split("/")[2]},general:{perPage:"100",include:"categories,sections"},async getAll(t=1){let e=`${this.web.subdomain}-articles-p${t}`;if(sessionStorage.getItem(e))return JSON.parse(sessionStorage.getItem(e));let s=await(await fetch(`/api/v2/help_center/${this.web.locale}/articles?per_page=${this.general.perPage}&include=${this.general.include}`)).json();for(;s.next_page;){let o=await(await fetch(s.next_page)).json();s.categories.push(...o.categories.filter(c=>s.categories.findIndex(p=>p.id===c.id)===-1)),s.sections.push(...o.sections.filter(c=>s.sections.findIndex(p=>p.id===c.id)===-1)),s.articles=s.articles.concat(o.articles),s.next_page=o.next_page}return sessionStorage.setItem(e,JSON.stringify(s)),s}};var u={articles:d};var a=class{constructor(e){this.api=e!=null?e:{}}use(e){let n=this.api[e];if(!n)throw new Error(`HCv4: "${e}" was not provided as a dependency. Pass it when creating the instance: create("v4", ["${e}"])`);return n}getAllArticles(){return this.use("articles").getAll()}};function m(t,e=[]){let n=null;if(e.length>0){let s={};for(let r of e)s[r]=u[r];n=s}switch(t){case"v3":return new i(n);case"v4":return new a(n);default:throw new Error(`Unsupported HC version: ${t}`)}}return x(C);})();
2
+ //# sourceMappingURL=index.global.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/dom/index.ts","../src/utils/index.ts","../src/v3/index.ts","../src/api/articles/index.ts","../src/api/index.ts","../src/v4/index.ts","../src/hc/factory.ts"],"sourcesContent":["export * from \"./dom\";\nexport * from \"./utils\";\nexport * from \"./hc\";\n","export function ready(fn: () => void): void {\n if (document.readyState !== \"loading\") {\n fn();\n } else {\n document.addEventListener(\"DOMContentLoaded\", fn);\n }\n}\n","export function noop(): void {}\n\nexport function ping(): string {\n const msg = \"hc-lib loaded successfully!\";\n console.log(msg);\n return msg;\n}\n\nexport function timeout(ms: number): Promise<never> {\n return new Promise((_, reject) => setTimeout(() => reject(new Error(\"Request timeout\")), ms));\n}\n","import type { APIDependencies, HCInstance } from \"../hc/types\";\n\nimport type { deps } from \"../api\";\n\ntype ApiMap = Partial<{ [K in APIDependencies]: (typeof deps)[K] }>;\nexport class HCv3 implements HCInstance {\n\n private api: ApiMap;\n\n constructor(apiDeps: ApiMap | null) {\n this.api = apiDeps ?? {};\n }\n\n private use<K extends APIDependencies>(dep: K): (typeof deps)[K] {\n const module = this.api[dep];\n if (!module) {\n throw new Error(\n `HCv4: \"${dep}\" was not provided as a dependency. Pass it when creating the instance: create(\"v4\", [\"${dep}\"])`\n );\n }\n\n return module as (typeof deps)[K];\n }\n\n\n getAllArticles(): Promise<any> {\n const articles = this.use(\"articles\");\n return articles.getAll();\n }\n}\n","export const articles = {\n web: {\n subdomain: window.location.host,\n locale: window.location.pathname.split(\"/\")[2]\n },\n\n general: {\n perPage: \"100\",\n include: \"categories,sections\"\n },\n\n async getAll(page: number = 1) {\n \n const storageName = `${this.web.subdomain}-articles-p${page}`;\n if (sessionStorage.getItem(storageName)) { return JSON.parse(sessionStorage.getItem(storageName) as string); }\n\n const response = await fetch(\n `/api/v2/help_center/${this.web.locale}/articles?per_page=${this.general.perPage}&include=${this.general.include}`,\n );\n\n const json = await response.json();\n\n while (json.next_page) {\n const nextResult = await fetch(json.next_page);\n const nextJson = await nextResult.json();\n\n json.categories.push(...nextJson.categories\n .filter((nextCategory: any) =>\n json.categories\n .findIndex((category: any) => category.id === nextCategory.id) === -1\n )\n );\n json.sections.push(...nextJson.sections\n .filter((nextSection: any) =>\n json.sections\n .findIndex((section: any) => section.id === nextSection.id) === -1\n )\n );\n json.articles = json.articles.concat(nextJson.articles);\n json.next_page = nextJson.next_page;\n }\n\n sessionStorage.setItem(storageName, JSON.stringify(json));\n return json\n }\n}","import { articles } from \"./articles\";\n\nexport const deps = {\n articles,\n};","import type { APIDependencies, HCInstance } from \"../hc/types\";\nimport type { deps } from \"../api\";\n\ntype ApiMap = Partial<{ [K in APIDependencies]: (typeof deps)[K] }>;\n\nexport class HCv4 implements HCInstance {\n private api: ApiMap;\n\n constructor(apiDeps: ApiMap | null) {\n this.api = apiDeps ?? {};\n }\n\n private use<K extends APIDependencies>(dep: K): (typeof deps)[K] {\n const module = this.api[dep];\n if (!module) {\n throw new Error(\n `HCv4: \"${dep}\" was not provided as a dependency. Pass it when creating the instance: create(\"v4\", [\"${dep}\"])`\n );\n }\n\n return module as (typeof deps)[K];\n }\n\n getAllArticles(): Promise<any> {\n const articles = this.use(\"articles\");\n return articles.getAll();\n }\n}\n","import type { APIDependencies, HCInstance, HCVersion } from \"./types\";\nimport { HCv3 } from \"../v3\";\nimport { deps } from \"../api/index\"\nimport { HCv4 } from \"../v4\";\n\nexport function create(version: HCVersion, apiDependencies: APIDependencies[] = []): HCInstance {\n let api: Partial<Record<APIDependencies, any>> | null = null\n\n if (apiDependencies.length > 0) {\n const result: Partial<Record<APIDependencies, any>> = {}\n for (const dep of apiDependencies) result[dep] = deps[dep]\n api = result\n }\n\n switch (version) {\n case \"v3\":\n return new HCv3(api);\n case \"v4\":\n return new HCv4(api);\n default:\n throw new Error(`Unsupported HC version: ${version}`);\n }\n}\n"],"mappings":"2bAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,YAAAE,EAAA,SAAAC,EAAA,SAAAC,EAAA,UAAAC,EAAA,YAAAC,ICAO,SAASC,EAAMC,EAAsB,CACtC,SAAS,aAAe,UAC1BA,EAAG,EAEH,SAAS,iBAAiB,mBAAoBA,CAAE,CAEpD,CCNO,SAASC,GAAa,CAAC,CAEvB,SAASC,GAAe,CAC7B,IAAMC,EAAM,8BACZ,eAAQ,IAAIA,CAAG,EACRA,CACT,CAEO,SAASC,EAAQC,EAA4B,CAClD,OAAO,IAAI,QAAQ,CAACC,EAAGC,IAAW,WAAW,IAAMA,EAAO,IAAI,MAAM,iBAAiB,CAAC,EAAGF,CAAE,CAAC,CAC9F,CCLO,IAAMG,EAAN,KAAiC,CAItC,YAAYC,EAAwB,CAClC,KAAK,IAAMA,GAAA,KAAAA,EAAW,CAAC,CACzB,CAEQ,IAA+BC,EAA0B,CAC/D,IAAMC,EAAS,KAAK,IAAID,CAAG,EAC3B,GAAI,CAACC,EACH,MAA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,CACzB,CAEQ,IAA+BC,EAA0B,CAC/D,IAAMC,EAAS,KAAK,IAAID,CAAG,EAC3B,GAAI,CAACC,EACH,MAAM,IAAI,MACR,UAAUD,CAAG,0FAA0FA,CAAG,KAC5G,EAGF,OAAOC,CACT,CAEA,gBAA+B,CAE7B,OADiB,KAAK,IAAI,UAAU,EACpB,OAAO,CACzB,CACF,ECtBO,SAASC,EAAOC,EAAoBC,EAAqC,CAAC,EAAe,CAC9F,IAAIC,EAAoD,KAExD,GAAID,EAAgB,OAAS,EAAG,CAC9B,IAAME,EAAgD,CAAC,EACvD,QAAWC,KAAOH,EAAiBE,EAAOC,CAAG,EAAIC,EAAKD,CAAG,EACzDF,EAAMC,CACR,CAEA,OAAQH,EAAS,CACf,IAAK,KACH,OAAO,IAAIM,EAAKJ,CAAG,EACrB,IAAK,KACH,OAAO,IAAIK,EAAKL,CAAG,EACrB,QACE,MAAM,IAAI,MAAM,2BAA2BF,CAAO,EAAE,CACxD,CACF","names":["index_exports","__export","create","noop","ping","ready","timeout","ready","fn","noop","ping","msg","timeout","ms","_","reject","HCv3","apiDeps","dep","module","articles","page","storageName","json","nextJson","nextCategory","category","nextSection","section","deps","articles","HCv4","apiDeps","dep","module","create","version","apiDependencies","api","result","dep","deps","HCv3","HCv4"]}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var l=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var h=(t,e)=>{for(var n in e)l(t,n,{get:e[n],enumerable:!0})},w=(t,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of g(e))!y.call(t,r)&&r!==n&&l(t,r,{get:()=>e[r],enumerable:!(s=f(e,r))||s.enumerable});return t};var x=t=>w(l({},"__esModule",{value:!0}),t);var C={};h(C,{create:()=>m,noop:()=>I,ping:()=>P,ready:()=>A,timeout:()=>v});module.exports=x(C);function A(t){document.readyState!=="loading"?t():document.addEventListener("DOMContentLoaded",t)}function I(){}function P(){let t="hc-lib loaded successfully!";return console.log(t),t}function v(t){return new Promise((e,n)=>setTimeout(()=>n(new Error("Request timeout")),t))}var i=class{constructor(e){this.api=e!=null?e:{}}use(e){let n=this.api[e];if(!n)throw new Error(`HCv4: "${e}" was not provided as a dependency. Pass it when creating the instance: create("v4", ["${e}"])`);return n}getAllArticles(){return this.use("articles").getAll()}};var d={web:{subdomain:window.location.host,locale:window.location.pathname.split("/")[2]},general:{perPage:"100",include:"categories,sections"},async getAll(t=1){let e=`${this.web.subdomain}-articles-p${t}`;if(sessionStorage.getItem(e))return JSON.parse(sessionStorage.getItem(e));let s=await(await fetch(`/api/v2/help_center/${this.web.locale}/articles?per_page=${this.general.perPage}&include=${this.general.include}`)).json();for(;s.next_page;){let o=await(await fetch(s.next_page)).json();s.categories.push(...o.categories.filter(c=>s.categories.findIndex(p=>p.id===c.id)===-1)),s.sections.push(...o.sections.filter(c=>s.sections.findIndex(p=>p.id===c.id)===-1)),s.articles=s.articles.concat(o.articles),s.next_page=o.next_page}return sessionStorage.setItem(e,JSON.stringify(s)),s}};var u={articles:d};var a=class{constructor(e){this.api=e!=null?e:{}}use(e){let n=this.api[e];if(!n)throw new Error(`HCv4: "${e}" was not provided as a dependency. Pass it when creating the instance: create("v4", ["${e}"])`);return n}getAllArticles(){return this.use("articles").getAll()}};function m(t,e=[]){let n=null;if(e.length>0){let s={};for(let r of e)s[r]=u[r];n=s}switch(t){case"v3":return new i(n);case"v4":return new a(n);default:throw new Error(`Unsupported HC version: ${t}`)}}0&&(module.exports={create,noop,ping,ready,timeout});
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/dom/index.ts","../src/utils/index.ts","../src/v3/index.ts","../src/api/articles/index.ts","../src/api/index.ts","../src/v4/index.ts","../src/hc/factory.ts"],"sourcesContent":["export * from \"./dom\";\nexport * from \"./utils\";\nexport * from \"./hc\";\n","export function ready(fn: () => void): void {\n if (document.readyState !== \"loading\") {\n fn();\n } else {\n document.addEventListener(\"DOMContentLoaded\", fn);\n }\n}\n","export function noop(): void {}\n\nexport function ping(): string {\n const msg = \"hc-lib loaded successfully!\";\n console.log(msg);\n return msg;\n}\n\nexport function timeout(ms: number): Promise<never> {\n return new Promise((_, reject) => setTimeout(() => reject(new Error(\"Request timeout\")), ms));\n}\n","import type { APIDependencies, HCInstance } from \"../hc/types\";\n\nimport type { deps } from \"../api\";\n\ntype ApiMap = Partial<{ [K in APIDependencies]: (typeof deps)[K] }>;\nexport class HCv3 implements HCInstance {\n\n private api: ApiMap;\n\n constructor(apiDeps: ApiMap | null) {\n this.api = apiDeps ?? {};\n }\n\n private use<K extends APIDependencies>(dep: K): (typeof deps)[K] {\n const module = this.api[dep];\n if (!module) {\n throw new Error(\n `HCv4: \"${dep}\" was not provided as a dependency. Pass it when creating the instance: create(\"v4\", [\"${dep}\"])`\n );\n }\n\n return module as (typeof deps)[K];\n }\n\n\n getAllArticles(): Promise<any> {\n const articles = this.use(\"articles\");\n return articles.getAll();\n }\n}\n","export const articles = {\n web: {\n subdomain: window.location.host,\n locale: window.location.pathname.split(\"/\")[2]\n },\n\n general: {\n perPage: \"100\",\n include: \"categories,sections\"\n },\n\n async getAll(page: number = 1) {\n \n const storageName = `${this.web.subdomain}-articles-p${page}`;\n if (sessionStorage.getItem(storageName)) { return JSON.parse(sessionStorage.getItem(storageName) as string); }\n\n const response = await fetch(\n `/api/v2/help_center/${this.web.locale}/articles?per_page=${this.general.perPage}&include=${this.general.include}`,\n );\n\n const json = await response.json();\n\n while (json.next_page) {\n const nextResult = await fetch(json.next_page);\n const nextJson = await nextResult.json();\n\n json.categories.push(...nextJson.categories\n .filter((nextCategory: any) =>\n json.categories\n .findIndex((category: any) => category.id === nextCategory.id) === -1\n )\n );\n json.sections.push(...nextJson.sections\n .filter((nextSection: any) =>\n json.sections\n .findIndex((section: any) => section.id === nextSection.id) === -1\n )\n );\n json.articles = json.articles.concat(nextJson.articles);\n json.next_page = nextJson.next_page;\n }\n\n sessionStorage.setItem(storageName, JSON.stringify(json));\n return json\n }\n}","import { articles } from \"./articles\";\n\nexport const deps = {\n articles,\n};","import type { APIDependencies, HCInstance } from \"../hc/types\";\nimport type { deps } from \"../api\";\n\ntype ApiMap = Partial<{ [K in APIDependencies]: (typeof deps)[K] }>;\n\nexport class HCv4 implements HCInstance {\n private api: ApiMap;\n\n constructor(apiDeps: ApiMap | null) {\n this.api = apiDeps ?? {};\n }\n\n private use<K extends APIDependencies>(dep: K): (typeof deps)[K] {\n const module = this.api[dep];\n if (!module) {\n throw new Error(\n `HCv4: \"${dep}\" was not provided as a dependency. Pass it when creating the instance: create(\"v4\", [\"${dep}\"])`\n );\n }\n\n return module as (typeof deps)[K];\n }\n\n getAllArticles(): Promise<any> {\n const articles = this.use(\"articles\");\n return articles.getAll();\n }\n}\n","import type { APIDependencies, HCInstance, HCVersion } from \"./types\";\nimport { HCv3 } from \"../v3\";\nimport { deps } from \"../api/index\"\nimport { HCv4 } from \"../v4\";\n\nexport function create(version: HCVersion, apiDependencies: APIDependencies[] = []): HCInstance {\n let api: Partial<Record<APIDependencies, any>> | null = null\n\n if (apiDependencies.length > 0) {\n const result: Partial<Record<APIDependencies, any>> = {}\n for (const dep of apiDependencies) result[dep] = deps[dep]\n api = result\n }\n\n switch (version) {\n case \"v3\":\n return new HCv3(api);\n case \"v4\":\n return new HCv4(api);\n default:\n throw new Error(`Unsupported HC version: ${version}`);\n }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,YAAAE,EAAA,SAAAC,EAAA,SAAAC,EAAA,UAAAC,EAAA,YAAAC,IAAA,eAAAC,EAAAP,GCAO,SAASQ,EAAMC,EAAsB,CACtC,SAAS,aAAe,UAC1BA,EAAG,EAEH,SAAS,iBAAiB,mBAAoBA,CAAE,CAEpD,CCNO,SAASC,GAAa,CAAC,CAEvB,SAASC,GAAe,CAC7B,IAAMC,EAAM,8BACZ,eA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J,EAAK,SACF,UAAWK,GAAiBA,EAAQ,KAAOD,EAAY,EAAE,IAAM,EACpE,CACF,EACAJ,EAAK,SAAWA,EAAK,SAAS,OAAOC,EAAS,QAAQ,EACtDD,EAAK,UAAYC,EAAS,SAC5B,CAEA,sBAAe,QAAQF,EAAa,KAAK,UAAUC,CAAI,CAAC,EACjDA,CACT,CACF,EC3CO,IAAMM,EAAO,CAClB,SAAAC,CACF,ECCO,IAAMC,EAAN,KAAiC,CAGtC,YAAYC,EAAwB,CAClC,KAAK,IAAMA,GAAA,KAAAA,EAAW,CAAC,CACzB,CAEQ,IAA+BC,EAA0B,CAC/D,IAAMC,EAAS,KAAK,IAAID,CAAG,EAC3B,GAAI,CAACC,EACH,MAAM,IAAI,MACR,UAAUD,CAAG,0FAA0FA,CAAG,KAC5G,EAGF,OAAOC,CACT,CAEA,gBAA+B,CAE7B,OADiB,KAAK,IAAI,UAAU,EACpB,OAAO,CACzB,CACF,ECtBO,SAASC,EAAOC,EAAoBC,EAAqC,CAAC,EAAe,CAC9F,IAAIC,EAAoD,KAExD,GAAID,EAAgB,OAAS,EAAG,CAC9B,IAAME,EAAgD,CAAC,EACvD,QAAWC,KAAOH,EAAiBE,EAAOC,CAAG,EAAIC,EAAKD,CAAG,EACzDF,EAAMC,CACR,CAEA,OAAQH,EAAS,CACf,IAAK,KACH,OAAO,IAAIM,EAAKJ,CAAG,EACrB,IAAK,KACH,OAAO,IAAIK,EAAKL,CAAG,EACrB,QACE,MAAM,IAAI,MAAM,2BAA2BF,CAAO,EAAE,CACxD,CACF","names":["index_exports","__export","create","noop","ping","ready","timeout","__toCommonJS","ready","fn","noop","ping","msg","timeout","ms","_","reject","HCv3","apiDeps","dep","module","articles","page","storageName","json","nextJson","nextCategory","category","nextSection","section","deps","articles","HCv4","apiDeps","dep","module","create","version","apiDependencies","api","result","dep","deps","HCv3","HCv4"]}
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ function m(t){document.readyState!=="loading"?t():document.addEventListener("DOMContentLoaded",t)}function g(){}function y(){let t="hc-lib loaded successfully!";return console.log(t),t}function h(t){return new Promise((e,s)=>setTimeout(()=>s(new Error("Request timeout")),t))}var o=class{constructor(e){this.api=e!=null?e:{}}use(e){let s=this.api[e];if(!s)throw new Error(`HCv4: "${e}" was not provided as a dependency. Pass it when creating the instance: create("v4", ["${e}"])`);return s}getAllArticles(){return this.use("articles").getAll()}};var l={web:{subdomain:window.location.host,locale:window.location.pathname.split("/")[2]},general:{perPage:"100",include:"categories,sections"},async getAll(t=1){let e=`${this.web.subdomain}-articles-p${t}`;if(sessionStorage.getItem(e))return JSON.parse(sessionStorage.getItem(e));let n=await(await fetch(`/api/v2/help_center/${this.web.locale}/articles?per_page=${this.general.perPage}&include=${this.general.include}`)).json();for(;n.next_page;){let r=await(await fetch(n.next_page)).json();n.categories.push(...r.categories.filter(c=>n.categories.findIndex(p=>p.id===c.id)===-1)),n.sections.push(...r.sections.filter(c=>n.sections.findIndex(p=>p.id===c.id)===-1)),n.articles=n.articles.concat(r.articles),n.next_page=r.next_page}return sessionStorage.setItem(e,JSON.stringify(n)),n}};var d={articles:l};var i=class{constructor(e){this.api=e!=null?e:{}}use(e){let s=this.api[e];if(!s)throw new Error(`HCv4: "${e}" was not provided as a dependency. Pass it when creating the instance: create("v4", ["${e}"])`);return s}getAllArticles(){return this.use("articles").getAll()}};function u(t,e=[]){let s=null;if(e.length>0){let n={};for(let a of e)n[a]=d[a];s=n}switch(t){case"v3":return new o(s);case"v4":return new i(s);default:throw new Error(`Unsupported HC version: ${t}`)}}export{u as create,g as noop,y as ping,m as ready,h as timeout};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/dom/index.ts","../src/utils/index.ts","../src/v3/index.ts","../src/api/articles/index.ts","../src/api/index.ts","../src/v4/index.ts","../src/hc/factory.ts"],"sourcesContent":["export function ready(fn: () => void): void {\n if (document.readyState !== \"loading\") {\n fn();\n } else {\n document.addEventListener(\"DOMContentLoaded\", fn);\n }\n}\n","export function noop(): void {}\n\nexport function ping(): string {\n const msg = \"hc-lib loaded successfully!\";\n console.log(msg);\n return msg;\n}\n\nexport function timeout(ms: number): Promise<never> {\n return new Promise((_, reject) => setTimeout(() => reject(new Error(\"Request timeout\")), ms));\n}\n","import type { APIDependencies, HCInstance } from \"../hc/types\";\n\nimport type { deps } from \"../api\";\n\ntype ApiMap = Partial<{ [K in APIDependencies]: (typeof deps)[K] }>;\nexport class HCv3 implements HCInstance {\n\n private api: ApiMap;\n\n constructor(apiDeps: ApiMap | null) {\n this.api = apiDeps ?? {};\n }\n\n private use<K extends APIDependencies>(dep: K): (typeof deps)[K] {\n const module = this.api[dep];\n if (!module) {\n throw new Error(\n `HCv4: \"${dep}\" was not provided as a dependency. Pass it when creating the instance: create(\"v4\", [\"${dep}\"])`\n );\n }\n\n return module as (typeof deps)[K];\n }\n\n\n getAllArticles(): Promise<any> {\n const articles = this.use(\"articles\");\n return articles.getAll();\n }\n}\n","export const articles = {\n web: {\n subdomain: window.location.host,\n locale: window.location.pathname.split(\"/\")[2]\n },\n\n general: {\n perPage: \"100\",\n include: \"categories,sections\"\n },\n\n async getAll(page: number = 1) {\n \n const storageName = `${this.web.subdomain}-articles-p${page}`;\n if (sessionStorage.getItem(storageName)) { return JSON.parse(sessionStorage.getItem(storageName) as string); }\n\n const response = await fetch(\n `/api/v2/help_center/${this.web.locale}/articles?per_page=${this.general.perPage}&include=${this.general.include}`,\n );\n\n const json = await response.json();\n\n while (json.next_page) {\n const nextResult = await fetch(json.next_page);\n const nextJson = await nextResult.json();\n\n json.categories.push(...nextJson.categories\n .filter((nextCategory: any) =>\n json.categories\n .findIndex((category: any) => category.id === nextCategory.id) === -1\n )\n );\n json.sections.push(...nextJson.sections\n .filter((nextSection: any) =>\n json.sections\n .findIndex((section: any) => section.id === nextSection.id) === -1\n )\n );\n json.articles = json.articles.concat(nextJson.articles);\n json.next_page = nextJson.next_page;\n }\n\n sessionStorage.setItem(storageName, JSON.stringify(json));\n return json\n }\n}","import { articles } from \"./articles\";\n\nexport const deps = {\n articles,\n};","import type { APIDependencies, HCInstance } from \"../hc/types\";\nimport type { deps } from \"../api\";\n\ntype ApiMap = Partial<{ [K in APIDependencies]: (typeof deps)[K] }>;\n\nexport class HCv4 implements HCInstance {\n private api: ApiMap;\n\n constructor(apiDeps: ApiMap | null) {\n this.api = apiDeps ?? {};\n }\n\n private use<K extends APIDependencies>(dep: K): (typeof deps)[K] {\n const module = this.api[dep];\n if (!module) {\n throw new Error(\n `HCv4: \"${dep}\" was not provided as a dependency. Pass it when creating the instance: create(\"v4\", [\"${dep}\"])`\n );\n }\n\n return module as (typeof deps)[K];\n }\n\n getAllArticles(): Promise<any> {\n const articles = this.use(\"articles\");\n return articles.getAll();\n }\n}\n","import type { APIDependencies, HCInstance, HCVersion } from \"./types\";\nimport { HCv3 } from \"../v3\";\nimport { deps } from \"../api/index\"\nimport { HCv4 } from \"../v4\";\n\nexport function create(version: HCVersion, apiDependencies: APIDependencies[] = []): HCInstance {\n let api: Partial<Record<APIDependencies, any>> | null = null\n\n if (apiDependencies.length > 0) {\n const result: Partial<Record<APIDependencies, any>> = {}\n for (const dep of apiDependencies) result[dep] = deps[dep]\n api = result\n }\n\n switch (version) {\n case \"v3\":\n return new HCv3(api);\n case \"v4\":\n return new HCv4(api);\n default:\n throw new Error(`Unsupported HC version: ${version}`);\n }\n}\n"],"mappings":"AAAO,SAASA,EAAMC,EAAsB,CACtC,SAAS,aAAe,UAC1BA,EAAG,EAEH,SAAS,iBAAiB,mBAAoBA,CAAE,CAEpD,CCNO,SAASC,GAAa,CAAC,CAEvB,SAASC,GAAe,CAC7B,IAAMC,EAAM,8BACZ,eAAQ,IAAIA,CAAG,EACRA,CACT,CAEO,SAASC,EAAQC,EAA4B,CAClD,OAAO,IAAI,QAAQ,CAACC,EAAGC,IAAW,WAAW,IAAMA,EAAO,IAAI,MAAM,iBAAiB,CAAC,EAAGF,CAAE,CAAC,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,sBAAe,QAAQF,EAAa,KAAK,UAAUC,CAAI,CAAC,EACjDA,CACT,CACF,EC3CO,IAAMM,EAAO,CAClB,SAAAC,CACF,ECCO,IAAMC,EAAN,KAAiC,CAGtC,YAAYC,EAAwB,CAClC,KAAK,IAAMA,GAAA,KAAAA,EAAW,CAAC,CACzB,CAEQ,IAA+BC,EAA0B,CAC/D,IAAMC,EAAS,KAAK,IAAID,CAAG,EAC3B,GAAI,CAACC,EACH,MAAM,IAAI,MACR,UAAUD,CAAG,0FAA0FA,CAAG,KAC5G,EAGF,OAAOC,CACT,CAEA,gBAA+B,CAE7B,OADiB,KAAK,IAAI,UAAU,EACpB,OAAO,CACzB,CACF,ECtBO,SAASC,EAAOC,EAAoBC,EAAqC,CAAC,EAAe,CAC9F,IAAIC,EAAoD,KAExD,GAAID,EAAgB,OAAS,EAAG,CAC9B,IAAME,EAAgD,CAAC,EACvD,QAAWC,KAAOH,EAAiBE,EAAOC,CAAG,EAAIC,EAAKD,CAAG,EACzDF,EAAMC,CACR,CAEA,OAAQH,EAAS,CACf,IAAK,KACH,OAAO,IAAIM,EAAKJ,CAAG,EACrB,IAAK,KACH,OAAO,IAAIK,EAAKL,CAAG,EACrB,QACE,MAAM,IAAI,MAAM,2BAA2BF,CAAO,EAAE,CACxD,CACF","names":["ready","fn","noop","ping","msg","timeout","ms","_","reject","HCv3","apiDeps","dep","module","articles","page","storageName","json","nextJson","nextCategory","category","nextSection","section","deps","articles","HCv4","apiDeps","dep","module","create","version","apiDependencies","api","result","dep","deps","HCv3","HCv4"]}
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@matheusdev20/hc-lib",
3
+ "version": "0.1.2",
4
+ "description": "An typescript library to implement common function and utilities for Help Center development",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup",
13
+ "dev": "tsup --watch",
14
+ "test": "jest"
15
+ },
16
+ "keywords": [],
17
+ "author": "",
18
+ "license": "ISC",
19
+ "packageManager": "npm@10.28.2",
20
+ "devDependencies": {
21
+ "@types/jest": "^30.0.0",
22
+ "jest": "^30.2.0",
23
+ "jest-environment-jsdom": "^30.2.0",
24
+ "ts-jest": "^29.4.6",
25
+ "tsup": "^8.5.1",
26
+ "typescript": "^5.9.3"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ }
31
+ }