@dropins/tools 1.6.0-alpha5 → 1.6.0-alpha7

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/fetch-graphql.js CHANGED
@@ -1,4 +1,4 @@
1
1
  /*! Copyright 2025 Adobe
2
2
  All Rights Reserved. */
3
- var u=Object.defineProperty;var F=(r,e,t)=>e in r?u(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var a=(r,e,t)=>F(r,typeof e!="symbol"?e+"":e,t);const b={"Content-Type":"application/json",Accept:"application/json"};class p{constructor(){a(this,"_endpoint");a(this,"_parent");a(this,"_fetchGraphQlHeaders",{});a(this,"_beforeHooks",[]);a(this,"_afterHooks",[])}get endpoint(){return this._endpoint}get fetchGraphQlHeaders(){return this._fetchGraphQlHeaders}setEndpoint(e){this._endpoint=e}setFetchGraphQlHeader(e,t){this._fetchGraphQlHeaders={...this._fetchGraphQlHeaders,[e]:t}}removeFetchGraphQlHeader(e){delete this._fetchGraphQlHeaders[e]}getFetchGraphQlHeader(e){return this._fetchGraphQlHeaders[e]}setFetchGraphQlHeaders(e){typeof e=="function"?this._fetchGraphQlHeaders={...this._fetchGraphQlHeaders,...e(this._fetchGraphQlHeaders)}:this._fetchGraphQlHeaders={...e}}addBeforeHook(e){this._beforeHooks.push(e)}addAfterHook(e){this._afterHooks.push(e)}inheritFrom(e){return this._parent=e,this}async fetchGraphQl(e,t){const h=this.endpoint,n=this.fetchGraphQlHeaders;if(!h)throw Error('Missing "url"');const i=(t==null?void 0:t.method)??"POST",f=t==null?void 0:t.cache,G=t==null?void 0:t.signal;let H;const d=new URL(h),Q={...b,...n};i==="POST"&&(H=JSON.stringify({query:e,variables:t==null?void 0:t.variables})),i==="GET"&&(d.searchParams.append("query",m(e)),t!=null&&t.variables&&d.searchParams.append("variables",JSON.stringify(t.variables)));let s={method:i,headers:Q,body:H,cache:f,signal:G};s=await this._collectBeforeHooks().reduce(async(c,o)=>o(await c),Promise.resolve(s));const _=this._collectAfterHooks();return await fetch(d,s).then(c=>c.json().then(o=>_.reduce(async(g,k)=>k(s,await g),Promise.resolve(o))))}_collectBeforeHooks(){var t;return[...((t=this._parent)==null?void 0:t._collectBeforeHooks())??[],...this._beforeHooks]}_collectAfterHooks(){var t;const e=((t=this._parent)==null?void 0:t._collectAfterHooks())??[];return[...this._afterHooks,...e]}_collectParentHeaders(){return this._parent?{...this._parent._collectParentHeaders(),...this._parent._fetchGraphQlHeaders}:{}}getConfig(){return{endpoint:this.endpoint,fetchGraphQlHeaders:this.fetchGraphQlHeaders}}getMethods(){return{setEndpoint:this.setEndpoint.bind(this),setFetchGraphQlHeader:this.setFetchGraphQlHeader.bind(this),getFetchGraphQlHeader:this.getFetchGraphQlHeader.bind(this),removeFetchGraphQlHeader:this.removeFetchGraphQlHeader.bind(this),setFetchGraphQlHeaders:this.setFetchGraphQlHeaders.bind(this),fetchGraphQl:this.fetchGraphQl.bind(this),getConfig:this.getConfig.bind(this),addBeforeHook:this.addBeforeHook.bind(this),addAfterHook:this.addAfterHook.bind(this),inheritFrom:this.inheritFrom.bind(this)}}}const l=new p;class B extends p{get endpoint(){var e;return this._endpoint??((e=this._parent)==null?void 0:e.endpoint)??l.endpoint}get fetchGraphQlHeaders(){var t;const e=this._fetchGraphQlHeaders;if(this._endpoint)return{...this._collectParentHeaders(),...e};{const h=l.fetchGraphQlHeaders,n=((t=this._parent)==null?void 0:t.fetchGraphQlHeaders)??{};return{...h,...n,...e}}}}function m(r){return r=r.replace(/#.*/g,""),r=r.replace(/\s+/g," "),r.trim()}const{setEndpoint:P,setFetchGraphQlHeaders:w,setFetchGraphQlHeader:E,getFetchGraphQlHeader:y,removeFetchGraphQlHeader:C,fetchGraphQl:M,getConfig:O,addBeforeHook:S,addAfterHook:T,inheritFrom:j}=l.getMethods();export{B as FetchGraphQL,T as addAfterHook,S as addBeforeHook,M as fetchGraphQl,O as getConfig,y as getFetchGraphQlHeader,j as inheritFrom,C as removeFetchGraphQlHeader,P as setEndpoint,E as setFetchGraphQlHeader,w as setFetchGraphQlHeaders};
3
+ var F=Object.defineProperty;var g=(r,t,e)=>t in r?F(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var s=(r,t,e)=>g(r,typeof t!="symbol"?t+"":t,e);const b={"Content-Type":"application/json",Accept:"application/json"};class f{constructor(){s(this,"_endpoint");s(this,"_fetchGraphQlHeaders",{});s(this,"_beforeHooks",[]);s(this,"_afterHooks",[])}get endpoint(){return this._endpoint}get fetchGraphQlHeaders(){return this._fetchGraphQlHeaders}setEndpoint(t){this._endpoint=t}setFetchGraphQlHeader(t,e){this._fetchGraphQlHeaders={...this._fetchGraphQlHeaders,[t]:e}}removeFetchGraphQlHeader(t){delete this._fetchGraphQlHeaders[t]}getFetchGraphQlHeader(t){return this._fetchGraphQlHeaders[t]}setFetchGraphQlHeaders(t){typeof t=="function"?this._fetchGraphQlHeaders={...this._fetchGraphQlHeaders,...t(this._fetchGraphQlHeaders)}:this._fetchGraphQlHeaders={...t}}addBeforeHook(t){this._beforeHooks.push(t)}addAfterHook(t){this._afterHooks.push(t)}_collectBeforeHooks(){return this._beforeHooks}_collectAfterHooks(){return this._afterHooks}async fetchGraphQl(t,e){const h=this.endpoint,p=this.fetchGraphQlHeaders;if(!h)throw Error('Missing "url"');const d=(e==null?void 0:e.method)??"POST",_=e==null?void 0:e.cache,G=e==null?void 0:e.signal;let n;const i=new URL(h),Q={...b,...p};d==="POST"&&(n=JSON.stringify({query:t,variables:e==null?void 0:e.variables})),d==="GET"&&(i.searchParams.append("query",v(t)),e!=null&&e.variables&&i.searchParams.append("variables",JSON.stringify(e.variables)));let a={method:d,headers:Q,body:n,cache:_,signal:G};a=await this._collectBeforeHooks().reduce(async(c,o)=>o(await c),Promise.resolve(a));const u=this._collectAfterHooks();return await fetch(i,a).then(c=>c.json().then(o=>u.reduce(async(k,m)=>m(a,await k),Promise.resolve(o))))}getConfig(){return{endpoint:this.endpoint,fetchGraphQlHeaders:this.fetchGraphQlHeaders}}getMethods(){return{setEndpoint:this.setEndpoint.bind(this),setFetchGraphQlHeader:this.setFetchGraphQlHeader.bind(this),getFetchGraphQlHeader:this.getFetchGraphQlHeader.bind(this),removeFetchGraphQlHeader:this.removeFetchGraphQlHeader.bind(this),setFetchGraphQlHeaders:this.setFetchGraphQlHeaders.bind(this),fetchGraphQl:this.fetchGraphQl.bind(this),getConfig:this.getConfig.bind(this),addBeforeHook:this.addBeforeHook.bind(this),addAfterHook:this.addAfterHook.bind(this)}}}const l=new f;class H extends f{constructor(){super(...arguments);s(this,"_mode","default");s(this,"_source")}get endpoint(){var e;switch(this._mode){case"standalone":return this._source;case"linked":return(e=this._source)==null?void 0:e.endpoint;case"default":return l.endpoint}}get fetchGraphQlHeaders(){switch(this._mode){case"standalone":return this._fetchGraphQlHeaders;case"linked":return this._source.fetchGraphQlHeaders;case"default":return{...l.fetchGraphQlHeaders,...this._fetchGraphQlHeaders}}}setEndpoint(e){e instanceof H?(this._mode="linked",this._source=e):(this._mode="standalone",this._source=e)}setFetchGraphQlHeader(e,h){this._mode==="linked"?this._source.setFetchGraphQlHeader(e,h):super.setFetchGraphQlHeader(e,h)}setFetchGraphQlHeaders(e){this._mode==="linked"?this._source.setFetchGraphQlHeaders(e):super.setFetchGraphQlHeaders(e)}removeFetchGraphQlHeader(e){this._mode==="linked"?this._source.removeFetchGraphQlHeader(e):super.removeFetchGraphQlHeader(e)}getFetchGraphQlHeader(e){return this._mode==="linked"?this._source.getFetchGraphQlHeader(e):super.getFetchGraphQlHeader(e)}addBeforeHook(e){this._mode==="linked"?this._source.addBeforeHook(e):super.addBeforeHook(e)}addAfterHook(e){this._mode==="linked"?this._source.addAfterHook(e):super.addAfterHook(e)}_collectBeforeHooks(){return this._mode==="linked"?this._source._collectBeforeHooks():this._beforeHooks}_collectAfterHooks(){return this._mode==="linked"?this._source._collectAfterHooks():this._afterHooks}}function v(r){return r=r.replace(/#.*/g,""),r=r.replace(/\s+/g," "),r.trim()}const{setEndpoint:w,setFetchGraphQlHeaders:E,setFetchGraphQlHeader:P,getFetchGraphQlHeader:C,removeFetchGraphQlHeader:M,fetchGraphQl:O,getConfig:S,addBeforeHook:T,addAfterHook:j}=l.getMethods();export{H as FetchGraphQL,j as addAfterHook,T as addBeforeHook,O as fetchGraphQl,S as getConfig,C as getFetchGraphQlHeader,M as removeFetchGraphQlHeader,w as setEndpoint,P as setFetchGraphQlHeader,E as setFetchGraphQlHeaders};
4
4
  //# sourceMappingURL=fetch-graphql.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetch-graphql.js","sources":["@dropins/tools/src/fetch-graphql/index.ts"],"sourcesContent":["/********************************************************************\n * Copyright 2024 Adobe\n * All Rights Reserved.\n *\n * NOTICE: Adobe permits you to use, modify, and distribute this \n * file in accordance with the terms of the Adobe license agreement \n * accompanying it. \n *******************************************************************/\n\nexport type Header = { [key: string]: string | null };\n\nexport type FetchOptions = {\n method?: 'GET' | 'POST';\n variables?: { [key: string]: any };\n signal?: AbortSignal;\n cache?:\n | 'default'\n | 'no-store'\n | 'reload'\n | 'no-cache'\n | 'force-cache'\n | 'only-if-cached';\n};\n\nexport type FetchQueryError = Array<{\n message: string;\n extensions: { category: string };\n}>;\n\nexport type BeforeHook = (requestInit: RequestInit) => RequestInit;\nexport type AfterHook<T = any> = (\n requestInit: RequestInit,\n response: { errors?: FetchQueryError; data: T }\n) => { errors?: FetchQueryError; data: T };\n\nconst defaultHeaders = {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n};\n\nclass FetchGraphQLMesh {\n public _endpoint?: string;\n\n protected _parent?: FetchGraphQLMesh;\n\n get endpoint() {\n return this._endpoint;\n }\n\n get fetchGraphQlHeaders() {\n return this._fetchGraphQlHeaders;\n }\n\n public _fetchGraphQlHeaders: Header = {};\n\n public _beforeHooks: BeforeHook[] = [];\n\n public _afterHooks: AfterHook[] = [];\n\n /**\n * Sets the GraphQL endpoint.\n * @param endpoint - The GraphQL endpoint.\n */\n public setEndpoint(endpoint: string) {\n this._endpoint = endpoint;\n }\n\n /**\n * Sets the GraphQL headers.\n * @param key - The key of the header.\n * @param value - The value of the header.\n */\n public setFetchGraphQlHeader(key: string, value: string | null) {\n this._fetchGraphQlHeaders = {\n ...this._fetchGraphQlHeaders,\n [key]: value,\n };\n }\n /**\n * Removes a specific GraphQL header.\n * @param key - The key of the header.\n */\n public removeFetchGraphQlHeader(key: string) {\n delete this._fetchGraphQlHeaders[key];\n }\n /**\n * Gets the value of a specific GraphQL header.\n * @param key - The key of the header.\n * @returns The value of the header, or undefined if not found.\n */\n public getFetchGraphQlHeader(key: string): string | null | undefined {\n return this._fetchGraphQlHeaders[key];\n }\n /**\n * Sets the GraphQL headers.\n * @param header - The header object or a function that returns a header object.\n * If a function is provided, it will be called with the previous headers.\n * The returned object will be merged with the previous headers.\n * @example\n * ```js\n * // set headers\n * setFetchGraphQlHeaders({ test: 'test' });\n * \n * // merge with previous headers\n * setFetchGraphQlHeaders((prev) => ({\n * ...prev,\n * test: 'test2',\n * }));\n * ```\n */\n public setFetchGraphQlHeaders(header: Header | ((prev: Header) => Header)) {\n if (typeof header === 'function') {\n this._fetchGraphQlHeaders = {\n ...this._fetchGraphQlHeaders,\n ...header(this._fetchGraphQlHeaders),\n };\n } else {\n this._fetchGraphQlHeaders = { ...header };\n }\n }\n /**\n * Adds a hook executed before the GraphQL call.\n * @param hook - The hook function.\n * @example\n * ```js\n * // add before hook\n * addBeforeHook((requestInit) => console.log('About to execute ' + requestInit.method + ' call.'));\n *\n * // modify the requestInit before executing the request\n * addBeforeHook((requestInit) => {method: requestInit.method, body: 'new body'});\n * ```\n */\n public addBeforeHook(hook: BeforeHook): void {\n this._beforeHooks.push(hook);\n }\n /**\n * Adds a hook executed before the GraphQL call.\n * @param hook - The hook function.\n * @example\n * ```js\n * // add before hook\n * addAfterHook((requestInit, response) => console.log(\n * 'The result of ' + requestInit.method + ' call is ' + response.json().body\n * ));\n *\n * // modify the response\n * addAfterHook((requestInit, response) => new Response(JSON.stringify({ ...response, modified: true }));\n * ```\n */\n public addAfterHook(hook: AfterHook): void {\n this._afterHooks.push(hook);\n }\n /**\n * Inherit configuration from another FetchGraphQL instance.\n * The current instance will inherit endpoint, headers, and hooks from the parent.\n * Settings can be overridden in the child instance.\n * @param parent - The parent instance to inherit from.\n * @returns this instance for chaining.\n * @example\n * ```js\n * const parentFetcher = new FetchGraphQL();\n * parentFetcher.setEndpoint('https://api.example.com/graphql');\n * parentFetcher.setFetchGraphQlHeader('Store', 'default');\n *\n * const childFetcher = new FetchGraphQL();\n * childFetcher.inheritFrom(parentFetcher);\n * childFetcher.setFetchGraphQlHeader('User-ID', '123'); // Adds to inherited headers\n * ```\n */\n public inheritFrom(parent: FetchGraphQLMesh): this {\n this._parent = parent;\n return this;\n }\n /**\n * Fetches GraphQL data.\n * @param query - The GraphQL query.\n * @param options - Optional configuration for the fetch request.\n * @returns\n */\n public async fetchGraphQl<T = any>(\n query: string,\n options?: FetchOptions\n ): Promise<{ errors?: FetchQueryError; data: T }> {\n const endpoint = this.endpoint;\n const fetchGraphQlHeaders = this.fetchGraphQlHeaders;\n\n if (!endpoint) throw Error('Missing \"url\"');\n\n const method = options?.method ?? 'POST';\n const cache = options?.cache;\n const signal = options?.signal;\n\n let body;\n const url = new URL(endpoint);\n const headers = {\n ...defaultHeaders,\n ...fetchGraphQlHeaders,\n };\n\n if (method === 'POST') {\n body = JSON.stringify({\n query,\n variables: options?.variables,\n });\n }\n\n if (method === 'GET') {\n url.searchParams.append('query', minimizeGraphQlQuery(query));\n\n if (options?.variables)\n url.searchParams.append('variables', JSON.stringify(options.variables));\n }\n\n let requestInit: RequestInit = {\n method,\n headers,\n body,\n cache,\n signal,\n };\n\n // Collect all before hooks from parent chain\n const allBeforeHooks = this._collectBeforeHooks();\n requestInit = await allBeforeHooks.reduce(\n async (prev, hook) => hook(await prev),\n Promise.resolve(requestInit)\n );\n\n // Collect all after hooks from parent chain (reversed for proper execution order)\n const allAfterHooks = this._collectAfterHooks();\n return await fetch(url, requestInit).then((r) => r.json().then(\n (response) => allAfterHooks.reduce(\n async (result, hook) => hook(requestInit, await result),\n Promise.resolve(response)\n )\n ));\n }\n \n /**\n * Collects all before hooks from the parent chain.\n * Parent hooks execute first, then child hooks.\n * @private\n */\n private _collectBeforeHooks(): BeforeHook[] {\n const parentHooks = this._parent?._collectBeforeHooks() ?? [];\n return [...parentHooks, ...this._beforeHooks];\n }\n \n /**\n * Collects all after hooks from the parent chain.\n * Child hooks execute first, then parent hooks.\n * @private\n */\n private _collectAfterHooks(): AfterHook[] {\n const parentHooks = this._parent?._collectAfterHooks() ?? [];\n return [...this._afterHooks, ...parentHooks];\n }\n \n /**\n * Collect only the headers from parent chain, excluding global mesh headers.\n * This is used when the instance has its own endpoint.\n * @protected\n */\n protected _collectParentHeaders(): Header {\n if (!this._parent) {\n return {};\n }\n \n // Recursively collect parent's parent headers first\n const grandparentHeaders = this._parent._collectParentHeaders();\n \n // Then merge with this parent's own headers\n return {\n ...grandparentHeaders,\n ...this._parent._fetchGraphQlHeaders,\n };\n }\n /**\n * Gets the configuration.\n */\n public getConfig() {\n return {\n endpoint: this.endpoint,\n fetchGraphQlHeaders: this.fetchGraphQlHeaders,\n };\n }\n\n public getMethods() {\n return {\n setEndpoint: this.setEndpoint.bind(this),\n setFetchGraphQlHeader: this.setFetchGraphQlHeader.bind(this),\n getFetchGraphQlHeader: this.getFetchGraphQlHeader.bind(this),\n removeFetchGraphQlHeader: this.removeFetchGraphQlHeader.bind(this),\n setFetchGraphQlHeaders: this.setFetchGraphQlHeaders.bind(this),\n fetchGraphQl: this.fetchGraphQl.bind(this),\n getConfig: this.getConfig.bind(this),\n addBeforeHook: this.addBeforeHook.bind(this),\n addAfterHook: this.addAfterHook.bind(this),\n inheritFrom: this.inheritFrom.bind(this),\n };\n }\n}\n\nconst mesh = new FetchGraphQLMesh();\n\n/**\n * `FetchGraphQL` is a class that extends `FetchGraphQLMesh`.\n * It provides methods to get the GraphQL endpoint and headers.\n *\n * @class\n *\n */\nexport class FetchGraphQL extends FetchGraphQLMesh {\n get endpoint() {\n return this._endpoint ?? this._parent?.endpoint ?? mesh.endpoint;\n }\n\n get fetchGraphQlHeaders() {\n // Collect headers from the chain: global mesh -> parent -> this instance\n // For backward compatibility: if instance has own endpoint, don't inherit from global mesh\n // but still inherit from explicitly set parent\n const ownHeaders = this._fetchGraphQlHeaders;\n \n if (this._endpoint) {\n // Has own endpoint: inherit from parent chain (recursively) but not from global mesh\n const parentHeaders = this._collectParentHeaders();\n return {\n ...parentHeaders,\n ...ownHeaders,\n };\n } else {\n // No own endpoint: inherit from both global mesh and parent\n const globalHeaders = mesh.fetchGraphQlHeaders;\n const parentHeaders = this._parent?.fetchGraphQlHeaders ?? {};\n return {\n ...globalHeaders,\n ...parentHeaders,\n ...ownHeaders,\n };\n }\n }\n}\n\nfunction minimizeGraphQlQuery(query: string) {\n // Remove comments\n query = query.replace(/#.*/g, '');\n\n // Remove extra spaces, tabs, and line breaks\n query = query.replace(/\\s+/g, ' ');\n\n return query.trim();\n}\n/**\n * Exports several methods from the `mesh` object.\n *\n * @property {Function} setEndpoint - Sets the GraphQL endpoint.\n * @property {Function} setFetchGraphQlHeaders - Sets the GraphQL headers.\n * @property {Function} setFetchGraphQlHeader - Sets a specific GraphQL header.\n * @property {Function} getFetchGraphQlHeader - Gets the value of a specific GraphQL header.\n * @property {Function} removeFetchGraphQlHeader - Removes a specific GraphQL header.\n * @property {Function} fetchGraphQl - Fetches GraphQL data.\n * @property {Function} getConfig - Gets the configuration.\n * @property {Function} addBeforeHook - Adds a hook executed before the GraphQL call.\n * @property {Function} addAfterHook - Adds a hook executed after the GraphQL call.\n * @property {Function} inheritFrom - Inherit configuration from another instance.\n */\n\n// Global Mesh instance\nexport const {\n setEndpoint,\n setFetchGraphQlHeaders,\n setFetchGraphQlHeader,\n getFetchGraphQlHeader,\n removeFetchGraphQlHeader,\n fetchGraphQl,\n getConfig,\n addBeforeHook,\n addAfterHook,\n inheritFrom,\n} = mesh.getMethods();\n"],"names":["defaultHeaders","FetchGraphQLMesh","__publicField","endpoint","key","value","header","hook","parent","query","options","fetchGraphQlHeaders","method","cache","signal","body","url","headers","minimizeGraphQlQuery","requestInit","prev","allAfterHooks","r","response","result","_a","parentHooks","mesh","FetchGraphQL","ownHeaders","globalHeaders","parentHeaders","setEndpoint","setFetchGraphQlHeaders","setFetchGraphQlHeader","getFetchGraphQlHeader","removeFetchGraphQlHeader","fetchGraphQl","getConfig","addBeforeHook","addAfterHook","inheritFrom"],"mappings":"oKAmCA,MAAMA,EAAiB,CACrB,eAAgB,mBAChB,OAAQ,kBACV,EAEA,MAAMC,CAAiB,CAAvB,cACSC,EAAA,kBAEGA,EAAA,gBAUHA,EAAA,4BAA+B,CAAC,GAEhCA,EAAA,oBAA6B,CAAC,GAE9BA,EAAA,mBAA2B,CAAC,GAZnC,IAAI,UAAW,CACb,OAAO,KAAK,SAAA,CAGd,IAAI,qBAAsB,CACxB,OAAO,KAAK,oBAAA,CAaP,YAAYC,EAAkB,CACnC,KAAK,UAAYA,CAAA,CAQZ,sBAAsBC,EAAaC,EAAsB,CAC9D,KAAK,qBAAuB,CAC1B,GAAG,KAAK,qBACR,CAACD,CAAG,EAAGC,CACT,CAAA,CAMK,yBAAyBD,EAAa,CACpC,OAAA,KAAK,qBAAqBA,CAAG,CAAA,CAO/B,sBAAsBA,EAAwC,CAC5D,OAAA,KAAK,qBAAqBA,CAAG,CAAA,CAmB/B,uBAAuBE,EAA6C,CACrE,OAAOA,GAAW,WACpB,KAAK,qBAAuB,CAC1B,GAAG,KAAK,qBACR,GAAGA,EAAO,KAAK,oBAAoB,CACrC,EAEK,KAAA,qBAAuB,CAAE,GAAGA,CAAO,CAC1C,CAcK,cAAcC,EAAwB,CACtC,KAAA,aAAa,KAAKA,CAAI,CAAA,CAgBtB,aAAaA,EAAuB,CACpC,KAAA,YAAY,KAAKA,CAAI,CAAA,CAmBrB,YAAYC,EAAgC,CACjD,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EnC,OAAAA,EAAAA,EAAM,QAAQ,OAAQ,EAAE,EAGxBA,EAAAA,EAAM,QAAQ,OAAQ,GAAG,EAE1BA,EAAM,KAAK,CACpB,CAiBa,KAAA,CACX,YAAAuB,EACA,uBAAAC,EACA,sBAAAC,EACA,sBAAAC,EACA,yBAAAC,EACA,aAAAC,EACA,UAAAC,EACA,cAAAC,EACA,aAAAC,EACA,YAAAC,CACF,EAAId,EAAK,WAAW"}
1
+ {"version":3,"file":"fetch-graphql.js","sources":["@dropins/tools/src/fetch-graphql/index.ts"],"sourcesContent":["/********************************************************************\n * Copyright 2024 Adobe\n * All Rights Reserved.\n *\n * NOTICE: Adobe permits you to use, modify, and distribute this \n * file in accordance with the terms of the Adobe license agreement \n * accompanying it. \n *******************************************************************/\n\nexport type Header = { [key: string]: string | null };\n\nexport type FetchOptions = {\n method?: 'GET' | 'POST';\n variables?: { [key: string]: any };\n signal?: AbortSignal;\n cache?:\n | 'default'\n | 'no-store'\n | 'reload'\n | 'no-cache'\n | 'force-cache'\n | 'only-if-cached';\n};\n\nexport type FetchQueryError = Array<{\n message: string;\n extensions: { category: string };\n}>;\n\nexport type BeforeHook = (requestInit: RequestInit) => RequestInit;\nexport type AfterHook<T = any> = (\n requestInit: RequestInit,\n response: { errors?: FetchQueryError; data: T }\n) => { errors?: FetchQueryError; data: T };\n\nconst defaultHeaders = {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n};\n\nclass FetchGraphQLMesh {\n protected _endpoint?: string;\n\n public _fetchGraphQlHeaders: Header = {};\n\n public _beforeHooks: BeforeHook[] = [];\n\n public _afterHooks: AfterHook[] = [];\n\n get endpoint() {\n return this._endpoint;\n }\n\n get fetchGraphQlHeaders() {\n return this._fetchGraphQlHeaders;\n }\n\n /**\n * Sets the GraphQL endpoint.\n * @param endpoint - The GraphQL endpoint URL string.\n * @example\n * ```js\n * // Set endpoint as string\n * instance.setEndpoint('https://api.example.com/graphql');\n * ```\n */\n public setEndpoint(endpoint: string) {\n this._endpoint = endpoint;\n }\n\n /**\n * Sets the GraphQL headers.\n * @param key - The key of the header.\n * @param value - The value of the header.\n */\n public setFetchGraphQlHeader(key: string, value: string | null) {\n this._fetchGraphQlHeaders = {\n ...this._fetchGraphQlHeaders,\n [key]: value,\n };\n }\n\n /**\n * Removes a specific GraphQL header.\n * @param key - The key of the header.\n */\n public removeFetchGraphQlHeader(key: string) {\n delete this._fetchGraphQlHeaders[key];\n }\n\n /**\n * Gets the value of a specific GraphQL header.\n * @param key - The key of the header.\n * @returns The value of the header, or undefined if not found.\n */\n public getFetchGraphQlHeader(key: string): string | null | undefined {\n return this._fetchGraphQlHeaders[key];\n }\n\n /**\n * Sets the GraphQL headers.\n * @param header - The header object or a function that returns a header object.\n * If a function is provided, it will be called with the previous headers.\n * The returned object will be merged with the previous headers.\n * @example\n * ```js\n * // set headers\n * setFetchGraphQlHeaders({ test: 'test' });\n * \n * // merge with previous headers\n * setFetchGraphQlHeaders((prev) => ({\n * ...prev,\n * test: 'test2',\n * }));\n * ```\n */\n public setFetchGraphQlHeaders(header: Header | ((prev: Header) => Header)) {\n if (typeof header === 'function') {\n this._fetchGraphQlHeaders = {\n ...this._fetchGraphQlHeaders,\n ...header(this._fetchGraphQlHeaders),\n };\n } else {\n this._fetchGraphQlHeaders = { ...header };\n }\n }\n\n /**\n * Adds a hook executed before the GraphQL call.\n * @param hook - The hook function.\n * @example\n * ```js\n * // add before hook\n * addBeforeHook((requestInit) => console.log('About to execute ' + requestInit.method + ' call.'));\n *\n * // modify the requestInit before executing the request\n * addBeforeHook((requestInit) => {method: requestInit.method, body: 'new body'});\n * ```\n */\n public addBeforeHook(hook: BeforeHook): void {\n this._beforeHooks.push(hook);\n }\n\n /**\n * Adds a hook executed before the GraphQL call.\n * @param hook - The hook function.\n * @example\n * ```js\n * // add before hook\n * addAfterHook((requestInit, response) => console.log(\n * 'The result of ' + requestInit.method + ' call is ' + response.json().body\n * ));\n *\n * // modify the response\n * addAfterHook((requestInit, response) => new Response(JSON.stringify({ ...response, modified: true }));\n * ```\n */\n public addAfterHook(hook: AfterHook): void {\n this._afterHooks.push(hook);\n }\n \n /**\n * Collects all before hooks. Can be overridden by subclasses for inheritance.\n * @protected\n */\n protected _collectBeforeHooks(): BeforeHook[] {\n return this._beforeHooks;\n }\n \n /**\n * Collects all after hooks. Can be overridden by subclasses for inheritance.\n * @protected\n */\n protected _collectAfterHooks(): AfterHook[] {\n return this._afterHooks;\n }\n \n /**\n * Fetches GraphQL data.\n * @param query - The GraphQL query.\n * @param options - Optional configuration for the fetch request.\n * @returns\n */\n public async fetchGraphQl<T = any>(\n query: string,\n options?: FetchOptions\n ): Promise<{ errors?: FetchQueryError; data: T }> {\n const endpoint = this.endpoint;\n const fetchGraphQlHeaders = this.fetchGraphQlHeaders;\n\n if (!endpoint) throw Error('Missing \"url\"');\n\n const method = options?.method ?? 'POST';\n const cache = options?.cache;\n const signal = options?.signal;\n\n let body;\n const url = new URL(endpoint);\n const headers = {\n ...defaultHeaders,\n ...fetchGraphQlHeaders,\n };\n\n if (method === 'POST') {\n body = JSON.stringify({\n query,\n variables: options?.variables,\n });\n }\n\n if (method === 'GET') {\n url.searchParams.append('query', minimizeGraphQlQuery(query));\n\n if (options?.variables)\n url.searchParams.append('variables', JSON.stringify(options.variables));\n }\n\n let requestInit: RequestInit = {\n method,\n headers,\n body,\n cache,\n signal,\n };\n\n // Collect and execute before hooks\n const allBeforeHooks = this._collectBeforeHooks();\n requestInit = await allBeforeHooks.reduce(\n async (prev, hook) => hook(await prev),\n Promise.resolve(requestInit)\n );\n\n // Collect and execute after hooks\n const allAfterHooks = this._collectAfterHooks();\n return await fetch(url, requestInit).then((r) => r.json().then(\n (response) => allAfterHooks.reduce(\n async (result, hook) => hook(requestInit, await result),\n Promise.resolve(response)\n )\n ));\n }\n \n /**\n * Gets the configuration.\n */\n public getConfig() {\n return {\n endpoint: this.endpoint,\n fetchGraphQlHeaders: this.fetchGraphQlHeaders,\n };\n }\n\n public getMethods() {\n return {\n setEndpoint: this.setEndpoint.bind(this),\n setFetchGraphQlHeader: this.setFetchGraphQlHeader.bind(this),\n getFetchGraphQlHeader: this.getFetchGraphQlHeader.bind(this),\n removeFetchGraphQlHeader: this.removeFetchGraphQlHeader.bind(this),\n setFetchGraphQlHeaders: this.setFetchGraphQlHeaders.bind(this),\n fetchGraphQl: this.fetchGraphQl.bind(this),\n getConfig: this.getConfig.bind(this),\n addBeforeHook: this.addBeforeHook.bind(this),\n addAfterHook: this.addAfterHook.bind(this),\n };\n }\n}\n\nconst mesh = new FetchGraphQLMesh();\n\n/**\n * `FetchGraphQL` is a class that extends `FetchGraphQLMesh`.\n * It provides methods to get the GraphQL endpoint and headers with support for inheritance.\n *\n * @class\n *\n */\nexport class FetchGraphQL extends FetchGraphQLMesh {\n private _mode: 'standalone' | 'linked' | 'default' = 'default';\n private _source?: string | FetchGraphQL;\n\n get endpoint(): string | undefined {\n switch (this._mode) {\n case 'standalone':\n return this._source as string;\n case 'linked':\n return (this._source as FetchGraphQL)?.endpoint;\n case 'default':\n return mesh.endpoint;\n }\n }\n\n get fetchGraphQlHeaders(): Header { \n switch (this._mode) {\n case 'standalone':\n // Standalone instance - only use own headers\n return this._fetchGraphQlHeaders;\n case 'linked':\n // Linked to another instance - use linked instance's headers directly\n return (this._source as FetchGraphQL).fetchGraphQlHeaders;\n case 'default':\n // Default - inherit from global mesh\n return {\n ...mesh.fetchGraphQlHeaders,\n ...this._fetchGraphQlHeaders,\n };\n }\n }\n\n /**\n * Sets the GraphQL endpoint or links to another FetchGraphQL instance.\n * @param endpoint - The GraphQL endpoint URL string, or a FetchGraphQL instance to link to.\n * @example\n * ```js\n * // Set endpoint as string\n * instance.setEndpoint('https://api.example.com/graphql');\n * \n * // Link to another instance\n * const parent = new FetchGraphQL();\n * parent.setEndpoint('https://api.example.com/graphql');\n * \n * const child = new FetchGraphQL();\n * child.setEndpoint(parent); // Links to parent, shares endpoint, headers, and hooks\n * ```\n */\n public setEndpoint(endpoint: string | FetchGraphQL): void {\n if (endpoint instanceof FetchGraphQL) {\n // Link to another instance\n this._mode = 'linked';\n this._source = endpoint;\n } else {\n // Set string endpoint - become standalone\n this._mode = 'standalone';\n this._source = endpoint;\n }\n }\n\n /**\n * Sets a GraphQL header. When linked to another instance, this sets the header on the linked instance.\n * @param key - The key of the header.\n * @param value - The value of the header.\n */\n public setFetchGraphQlHeader(key: string, value: string | null): void {\n if (this._mode === 'linked') {\n // Delegate to linked instance\n (this._source as FetchGraphQL).setFetchGraphQlHeader(key, value);\n } else {\n // Set on self\n super.setFetchGraphQlHeader(key, value);\n }\n }\n\n /**\n * Sets the GraphQL headers. When linked to another instance, this sets the headers on the linked instance.\n * @param header - The header object or a function that returns a header object.\n */\n public setFetchGraphQlHeaders(header: Header | ((prev: Header) => Header)): void {\n if (this._mode === 'linked') {\n // Delegate to linked instance\n (this._source as FetchGraphQL).setFetchGraphQlHeaders(header);\n } else {\n // Set on self\n super.setFetchGraphQlHeaders(header);\n }\n }\n\n /**\n * Removes a specific GraphQL header. When linked to another instance, this removes the header from the linked instance.\n * @param key - The key of the header.\n */\n public removeFetchGraphQlHeader(key: string): void {\n if (this._mode === 'linked') {\n // Delegate to linked instance\n (this._source as FetchGraphQL).removeFetchGraphQlHeader(key);\n } else {\n // Remove from self\n super.removeFetchGraphQlHeader(key);\n }\n }\n\n /**\n * Gets the value of a specific GraphQL header. When linked to another instance, this gets the header from the linked instance.\n * @param key - The key of the header.\n * @returns The value of the header, or undefined if not found.\n */\n public getFetchGraphQlHeader(key: string): string | null | undefined {\n if (this._mode === 'linked') {\n // Delegate to linked instance\n return (this._source as FetchGraphQL).getFetchGraphQlHeader(key);\n } else {\n // Get from self\n return super.getFetchGraphQlHeader(key);\n }\n }\n\n /**\n * Adds a before hook. When linked to another instance, this adds the hook to the linked instance.\n * @param hook - The hook function.\n */\n public addBeforeHook(hook: BeforeHook): void {\n if (this._mode === 'linked') {\n // Delegate to linked instance\n (this._source as FetchGraphQL).addBeforeHook(hook);\n } else {\n // Add to self\n super.addBeforeHook(hook);\n }\n }\n\n /**\n * Adds an after hook. When linked to another instance, this adds the hook to the linked instance.\n * @param hook - The hook function.\n */\n public addAfterHook(hook: AfterHook): void {\n if (this._mode === 'linked') {\n // Delegate to linked instance\n (this._source as FetchGraphQL).addAfterHook(hook);\n } else {\n // Add to self\n super.addAfterHook(hook);\n }\n }\n\n /**\n * Collects all before hooks. When linked, delegates to the linked instance.\n * @protected\n */\n protected _collectBeforeHooks(): BeforeHook[] {\n if (this._mode === 'linked') {\n return (this._source as FetchGraphQL)._collectBeforeHooks();\n }\n return this._beforeHooks;\n }\n \n /**\n * Collects all after hooks. When linked, delegates to the linked instance.\n * @protected\n */\n protected _collectAfterHooks(): AfterHook[] {\n if (this._mode === 'linked') {\n return (this._source as FetchGraphQL)._collectAfterHooks();\n }\n return this._afterHooks;\n }\n}\n\nfunction minimizeGraphQlQuery(query: string) {\n // Remove comments\n query = query.replace(/#.*/g, '');\n\n // Remove extra spaces, tabs, and line breaks\n query = query.replace(/\\s+/g, ' ');\n\n return query.trim();\n}\n/**\n * Exports several methods from the `mesh` object.\n *\n * @property {Function} setEndpoint - Sets the GraphQL endpoint.\n * @property {Function} setFetchGraphQlHeaders - Sets the GraphQL headers.\n * @property {Function} setFetchGraphQlHeader - Sets a specific GraphQL header.\n * @property {Function} getFetchGraphQlHeader - Gets the value of a specific GraphQL header.\n * @property {Function} removeFetchGraphQlHeader - Removes a specific GraphQL header.\n * @property {Function} fetchGraphQl - Fetches GraphQL data.\n * @property {Function} getConfig - Gets the configuration.\n * @property {Function} addBeforeHook - Adds a hook executed before the GraphQL call.\n * @property {Function} addAfterHook - Adds a hook executed after the GraphQL call.\n */\n\n// Global Mesh instance\nexport const {\n setEndpoint,\n setFetchGraphQlHeaders,\n setFetchGraphQlHeader,\n getFetchGraphQlHeader,\n removeFetchGraphQlHeader,\n fetchGraphQl,\n getConfig,\n addBeforeHook,\n addAfterHook,\n} = mesh.getMethods();\n"],"names":["defaultHeaders","FetchGraphQLMesh","__publicField","endpoint","key","value","header","hook","query","options","fetchGraphQlHeaders","method","cache","signal","body","url","headers","minimizeGraphQlQuery","requestInit","prev","allAfterHooks","r","response","result","mesh","FetchGraphQL","_a","setEndpoint","setFetchGraphQlHeaders","setFetchGraphQlHeader","getFetchGraphQlHeader","removeFetchGraphQlHeader","fetchGraphQl","getConfig","addBeforeHook","addAfterHook"],"mappings":"oKAmCA,MAAMA,EAAiB,CACrB,eAAgB,mBAChB,OAAQ,kBACV,EAEA,MAAMC,CAAiB,CAAvB,cACYC,EAAA,kBAEHA,EAAA,4BAA+B,CAAC,GAEhCA,EAAA,oBAA6B,CAAC,GAE9BA,EAAA,mBAA2B,CAAC,GAEnC,IAAI,UAAW,CACb,OAAO,KAAK,SAAA,CAGd,IAAI,qBAAsB,CACxB,OAAO,KAAK,oBAAA,CAYP,YAAYC,EAAkB,CACnC,KAAK,UAAYA,CAAA,CAQZ,sBAAsBC,EAAaC,EAAsB,CAC9D,KAAK,qBAAuB,CAC1B,GAAG,KAAK,qBACR,CAACD,CAAG,EAAGC,CACT,CAAA,CAOK,yBAAyBD,EAAa,CACpC,OAAA,KAAK,qBAAqBA,CAAG,CAAA,CAQ/B,sBAAsBA,EAAwC,CAC5D,OAAA,KAAK,qBAAqBA,CAAG,CAAA,CAoB/B,uBAAuBE,EAA6C,CACrE,OAAOA,GAAW,WACpB,KAAK,qBAAuB,CAC1B,GAAG,KAAK,qBACR,GAAGA,EAAO,KAAK,oBAAoB,CACrC,EAEK,KAAA,qBAAuB,CAAE,GAAGA,CAAO,CAC1C,CAeK,cAAcC,EAAwB,CACtC,KAAA,aAAa,KAAKA,CAAI,CAAA,CAiBtB,aAAaA,EAAuB,CACpC,KAAA,YAAY,KAAKA,CAAI,CAAA,CAOlB,qBAAoC,CAC5C,OAAO,KAAK,YAAA,CAOJ,oBAAkC,CAC1C,OAAO,KAAK,WAAA,CASd,MAAa,aACXC,EACAC,EACgD,CAChD,MAAMN,EAAW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QAAyB,sBAAsBA,CAAG,EAGxD,MAAM,sBAAsBA,CAAG,CACxC,CAOK,cAAcG,EAAwB,CACvC,KAAK,QAAU,SAEhB,KAAK,QAAyB,cAAcA,CAAI,EAGjD,MAAM,cAAcA,CAAI,CAC1B,CAOK,aAAaA,EAAuB,CACrC,KAAK,QAAU,SAEhB,KAAK,QAAyB,aAAaA,CAAI,EAGhD,MAAM,aAAaA,CAAI,CACzB,CAOQ,qBAAoC,CACxC,OAAA,KAAK,QAAU,SACT,KAAK,QAAyB,oBAAoB,EAErD,KAAK,YAAA,CAOJ,oBAAkC,CACtC,OAAA,KAAK,QAAU,SACT,KAAK,QAAyB,mBAAmB,EAEpD,KAAK,WAAA,CAEhB,CAEA,SAASU,EAAqBT,EAAe,CAEnC,OAAAA,EAAAA,EAAM,QAAQ,OAAQ,EAAE,EAGxBA,EAAAA,EAAM,QAAQ,OAAQ,GAAG,EAE1BA,EAAM,KAAK,CACpB,CAgBa,KAAA,CACX,YAAAmB,EACA,uBAAAC,EACA,sBAAAC,EACA,sBAAAC,EACA,yBAAAC,EACA,aAAAC,EACA,UAAAC,EACA,cAAAC,EACA,aAAAC,CACF,EAAIX,EAAK,WAAW"}
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name": "@dropins/tools", "version": "1.6.0-alpha5", "license": "SEE LICENSE IN LICENSE.md"}
1
+ {"name": "@dropins/tools", "version": "1.6.0-alpha7", "license": "SEE LICENSE IN LICENSE.md"}
@@ -0,0 +1,252 @@
1
+ /********************************************************************
2
+ * Copyright 2024 Adobe
3
+ * All Rights Reserved.
4
+ *
5
+ * NOTICE: Adobe permits you to use, modify, and distribute this
6
+ * file in accordance with the terms of the Adobe license agreement
7
+ * accompanying it.
8
+ *******************************************************************/
9
+ export type Header = {
10
+ [key: string]: string | null;
11
+ };
12
+ export type FetchOptions = {
13
+ method?: 'GET' | 'POST';
14
+ variables?: {
15
+ [key: string]: any;
16
+ };
17
+ signal?: AbortSignal;
18
+ cache?: 'default' | 'no-store' | 'reload' | 'no-cache' | 'force-cache' | 'only-if-cached';
19
+ };
20
+ export type FetchQueryError = Array<{
21
+ message: string;
22
+ extensions: {
23
+ category: string;
24
+ };
25
+ }>;
26
+ export type BeforeHook = (requestInit: RequestInit) => RequestInit;
27
+ export type AfterHook<T = any> = (requestInit: RequestInit, response: {
28
+ errors?: FetchQueryError;
29
+ data: T;
30
+ }) => {
31
+ errors?: FetchQueryError;
32
+ data: T;
33
+ };
34
+ declare class FetchGraphQLMesh {
35
+ protected _endpoint?: string;
36
+ _fetchGraphQlHeaders: Header;
37
+ _beforeHooks: BeforeHook[];
38
+ _afterHooks: AfterHook[];
39
+ get endpoint(): string | undefined;
40
+ get fetchGraphQlHeaders(): Header;
41
+ /**
42
+ * Sets the GraphQL endpoint.
43
+ * @param endpoint - The GraphQL endpoint URL string.
44
+ * @example
45
+ * ```js
46
+ * // Set endpoint as string
47
+ * instance.setEndpoint('https://api.example.com/graphql');
48
+ * ```
49
+ */
50
+ setEndpoint(endpoint: string): void;
51
+ /**
52
+ * Sets the GraphQL headers.
53
+ * @param key - The key of the header.
54
+ * @param value - The value of the header.
55
+ */
56
+ setFetchGraphQlHeader(key: string, value: string | null): void;
57
+ /**
58
+ * Removes a specific GraphQL header.
59
+ * @param key - The key of the header.
60
+ */
61
+ removeFetchGraphQlHeader(key: string): void;
62
+ /**
63
+ * Gets the value of a specific GraphQL header.
64
+ * @param key - The key of the header.
65
+ * @returns The value of the header, or undefined if not found.
66
+ */
67
+ getFetchGraphQlHeader(key: string): string | null | undefined;
68
+ /**
69
+ * Sets the GraphQL headers.
70
+ * @param header - The header object or a function that returns a header object.
71
+ * If a function is provided, it will be called with the previous headers.
72
+ * The returned object will be merged with the previous headers.
73
+ * @example
74
+ * ```js
75
+ * // set headers
76
+ * setFetchGraphQlHeaders({ test: 'test' });
77
+ *
78
+ * // merge with previous headers
79
+ * setFetchGraphQlHeaders((prev) => ({
80
+ * ...prev,
81
+ * test: 'test2',
82
+ * }));
83
+ * ```
84
+ */
85
+ setFetchGraphQlHeaders(header: Header | ((prev: Header) => Header)): void;
86
+ /**
87
+ * Adds a hook executed before the GraphQL call.
88
+ * @param hook - The hook function.
89
+ * @example
90
+ * ```js
91
+ * // add before hook
92
+ * addBeforeHook((requestInit) => console.log('About to execute ' + requestInit.method + ' call.'));
93
+ *
94
+ * // modify the requestInit before executing the request
95
+ * addBeforeHook((requestInit) => {method: requestInit.method, body: 'new body'});
96
+ * ```
97
+ */
98
+ addBeforeHook(hook: BeforeHook): void;
99
+ /**
100
+ * Adds a hook executed before the GraphQL call.
101
+ * @param hook - The hook function.
102
+ * @example
103
+ * ```js
104
+ * // add before hook
105
+ * addAfterHook((requestInit, response) => console.log(
106
+ * 'The result of ' + requestInit.method + ' call is ' + response.json().body
107
+ * ));
108
+ *
109
+ * // modify the response
110
+ * addAfterHook((requestInit, response) => new Response(JSON.stringify({ ...response, modified: true }));
111
+ * ```
112
+ */
113
+ addAfterHook(hook: AfterHook): void;
114
+ /**
115
+ * Collects all before hooks. Can be overridden by subclasses for inheritance.
116
+ * @protected
117
+ */
118
+ protected _collectBeforeHooks(): BeforeHook[];
119
+ /**
120
+ * Collects all after hooks. Can be overridden by subclasses for inheritance.
121
+ * @protected
122
+ */
123
+ protected _collectAfterHooks(): AfterHook[];
124
+ /**
125
+ * Fetches GraphQL data.
126
+ * @param query - The GraphQL query.
127
+ * @param options - Optional configuration for the fetch request.
128
+ * @returns
129
+ */
130
+ fetchGraphQl<T = any>(query: string, options?: FetchOptions): Promise<{
131
+ errors?: FetchQueryError;
132
+ data: T;
133
+ }>;
134
+ /**
135
+ * Gets the configuration.
136
+ */
137
+ getConfig(): {
138
+ endpoint: string | undefined;
139
+ fetchGraphQlHeaders: Header;
140
+ };
141
+ getMethods(): {
142
+ setEndpoint: (endpoint: string) => void;
143
+ setFetchGraphQlHeader: (key: string, value: string | null) => void;
144
+ getFetchGraphQlHeader: (key: string) => string | null | undefined;
145
+ removeFetchGraphQlHeader: (key: string) => void;
146
+ setFetchGraphQlHeaders: (header: Header | ((prev: Header) => Header)) => void;
147
+ fetchGraphQl: <T = any>(query: string, options?: FetchOptions | undefined) => Promise<{
148
+ errors?: FetchQueryError | undefined;
149
+ data: T;
150
+ }>;
151
+ getConfig: () => {
152
+ endpoint: string | undefined;
153
+ fetchGraphQlHeaders: Header;
154
+ };
155
+ addBeforeHook: (hook: BeforeHook) => void;
156
+ addAfterHook: (hook: AfterHook<any>) => void;
157
+ };
158
+ }
159
+ /**
160
+ * `FetchGraphQL` is a class that extends `FetchGraphQLMesh`.
161
+ * It provides methods to get the GraphQL endpoint and headers with support for inheritance.
162
+ *
163
+ * @class
164
+ *
165
+ */
166
+ export declare class FetchGraphQL extends FetchGraphQLMesh {
167
+ private _mode;
168
+ private _source?;
169
+ get endpoint(): string | undefined;
170
+ get fetchGraphQlHeaders(): Header;
171
+ /**
172
+ * Sets the GraphQL endpoint or links to another FetchGraphQL instance.
173
+ * @param endpoint - The GraphQL endpoint URL string, or a FetchGraphQL instance to link to.
174
+ * @example
175
+ * ```js
176
+ * // Set endpoint as string
177
+ * instance.setEndpoint('https://api.example.com/graphql');
178
+ *
179
+ * // Link to another instance
180
+ * const parent = new FetchGraphQL();
181
+ * parent.setEndpoint('https://api.example.com/graphql');
182
+ *
183
+ * const child = new FetchGraphQL();
184
+ * child.setEndpoint(parent); // Links to parent, shares endpoint, headers, and hooks
185
+ * ```
186
+ */
187
+ setEndpoint(endpoint: string | FetchGraphQL): void;
188
+ /**
189
+ * Sets a GraphQL header. When linked to another instance, this sets the header on the linked instance.
190
+ * @param key - The key of the header.
191
+ * @param value - The value of the header.
192
+ */
193
+ setFetchGraphQlHeader(key: string, value: string | null): void;
194
+ /**
195
+ * Sets the GraphQL headers. When linked to another instance, this sets the headers on the linked instance.
196
+ * @param header - The header object or a function that returns a header object.
197
+ */
198
+ setFetchGraphQlHeaders(header: Header | ((prev: Header) => Header)): void;
199
+ /**
200
+ * Removes a specific GraphQL header. When linked to another instance, this removes the header from the linked instance.
201
+ * @param key - The key of the header.
202
+ */
203
+ removeFetchGraphQlHeader(key: string): void;
204
+ /**
205
+ * Gets the value of a specific GraphQL header. When linked to another instance, this gets the header from the linked instance.
206
+ * @param key - The key of the header.
207
+ * @returns The value of the header, or undefined if not found.
208
+ */
209
+ getFetchGraphQlHeader(key: string): string | null | undefined;
210
+ /**
211
+ * Adds a before hook. When linked to another instance, this adds the hook to the linked instance.
212
+ * @param hook - The hook function.
213
+ */
214
+ addBeforeHook(hook: BeforeHook): void;
215
+ /**
216
+ * Adds an after hook. When linked to another instance, this adds the hook to the linked instance.
217
+ * @param hook - The hook function.
218
+ */
219
+ addAfterHook(hook: AfterHook): void;
220
+ /**
221
+ * Collects all before hooks. When linked, delegates to the linked instance.
222
+ * @protected
223
+ */
224
+ protected _collectBeforeHooks(): BeforeHook[];
225
+ /**
226
+ * Collects all after hooks. When linked, delegates to the linked instance.
227
+ * @protected
228
+ */
229
+ protected _collectAfterHooks(): AfterHook[];
230
+ }
231
+ /**
232
+ * Exports several methods from the `mesh` object.
233
+ *
234
+ * @property {Function} setEndpoint - Sets the GraphQL endpoint.
235
+ * @property {Function} setFetchGraphQlHeaders - Sets the GraphQL headers.
236
+ * @property {Function} setFetchGraphQlHeader - Sets a specific GraphQL header.
237
+ * @property {Function} getFetchGraphQlHeader - Gets the value of a specific GraphQL header.
238
+ * @property {Function} removeFetchGraphQlHeader - Removes a specific GraphQL header.
239
+ * @property {Function} fetchGraphQl - Fetches GraphQL data.
240
+ * @property {Function} getConfig - Gets the configuration.
241
+ * @property {Function} addBeforeHook - Adds a hook executed before the GraphQL call.
242
+ * @property {Function} addAfterHook - Adds a hook executed after the GraphQL call.
243
+ */
244
+ export declare const setEndpoint: (endpoint: string) => void, setFetchGraphQlHeaders: (header: Header | ((prev: Header) => Header)) => void, setFetchGraphQlHeader: (key: string, value: string | null) => void, getFetchGraphQlHeader: (key: string) => string | null | undefined, removeFetchGraphQlHeader: (key: string) => void, fetchGraphQl: <T = any>(query: string, options?: FetchOptions) => Promise<{
245
+ errors?: FetchQueryError | undefined;
246
+ data: T;
247
+ }>, getConfig: () => {
248
+ endpoint: string | undefined;
249
+ fetchGraphQlHeaders: Header;
250
+ }, addBeforeHook: (hook: BeforeHook) => void, addAfterHook: (hook: AfterHook) => void;
251
+ export {};
252
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,45 @@
1
+ import { ReCaptchaV3Response, PropsFormTypes, ReCaptchaV3Model } from './types/recaptcha.types';
2
+
3
+ export declare const recaptchaFetchApi: {
4
+ setEndpoint: (endpoint: string) => void;
5
+ setFetchGraphQlHeader: (key: string, value: string | null) => void;
6
+ getFetchGraphQlHeader: (key: string) => string | null | undefined;
7
+ removeFetchGraphQlHeader: (key: string) => void;
8
+ setFetchGraphQlHeaders: (header: import('@adobe-commerce/fetch-graphql').Header | ((prev: import('@adobe-commerce/fetch-graphql').Header) => import('@adobe-commerce/fetch-graphql').Header)) => void;
9
+ fetchGraphQl: <T = any>(query: string, options?: import('@adobe-commerce/fetch-graphql').FetchOptions | undefined) => Promise<{
10
+ errors?: import('@adobe-commerce/fetch-graphql').FetchQueryError | undefined;
11
+ data: T;
12
+ }>;
13
+ getConfig: () => {
14
+ endpoint: string | undefined;
15
+ fetchGraphQlHeaders: import('@adobe-commerce/fetch-graphql').Header;
16
+ };
17
+ addBeforeHook: (hook: import('@adobe-commerce/fetch-graphql').BeforeHook) => void;
18
+ addAfterHook: (hook: import('@adobe-commerce/fetch-graphql').AfterHook<any>) => void;
19
+ };
20
+ export declare class RecaptchaModule {
21
+ _enableReCAPTCHA: boolean;
22
+ _recaptchaBackendEndpoint: string;
23
+ _recaptchaScriptUrl: string;
24
+ _configStorageKey: string;
25
+ _logger: boolean;
26
+ _updateBadgePosition(badgeId: string, config: ReCaptchaV3Model): Promise<void | null>;
27
+ _addRecaptchaScript(): Promise<void>;
28
+ _fetchStoreConfig(): Promise<ReCaptchaV3Response | undefined>;
29
+ _loadConfig(): Promise<ReCaptchaV3Model | null>;
30
+ setEndpoint(url: string): void;
31
+ setConfig(configList: PropsFormTypes[]): Promise<void>;
32
+ initReCaptcha(lazyLoadTimeout?: number): Promise<void>;
33
+ verifyReCaptcha(): Promise<string | undefined>;
34
+ enableLogger(logger: boolean): void;
35
+ getMethods(): {
36
+ enableLogger: (logger: boolean) => void;
37
+ setEndpoint: (url: string) => void;
38
+ setConfig: (configList: PropsFormTypes[]) => Promise<void>;
39
+ initReCaptcha: (lazyLoadTimeout?: number) => Promise<void>;
40
+ verifyReCaptcha: () => Promise<string | undefined>;
41
+ };
42
+ }
43
+ declare const initReCaptcha: (lazyLoadTimeout?: number) => Promise<void>, verifyReCaptcha: () => Promise<string | undefined>, setEndpoint: (url: string) => void, setConfig: (configList: PropsFormTypes[]) => Promise<void>, enableLogger: (logger: boolean) => void;
44
+ export { setEndpoint, setConfig, initReCaptcha, verifyReCaptcha, enableLogger };
45
+ //# sourceMappingURL=index.d.ts.map