@dropins/tools 1.5.1-beta1 → 1.5.2-beta1
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
|
|
3
|
+
var m=Object.defineProperty;var b=(r,t,e)=>t in r?m(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var s=(r,t,e)=>b(r,typeof t!="symbol"?t+"":t,e);const F={"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 i=(e==null?void 0:e.method)??"POST",_=e==null?void 0:e.cache,G=e==null?void 0:e.signal;let l;const o=new URL(h),Q={...F,...p};i==="POST"&&(l=JSON.stringify({query:t,variables:e==null?void 0:e.variables})),i==="GET"&&(o.searchParams.append("query",v(t)),e!=null&&e.variables&&o.searchParams.append("variables",JSON.stringify(e.variables)));let a={method:i,headers:Q,body:l,cache:_,signal:G};a=await this._collectBeforeHooks().reduce(async(c,d)=>d(await c),Promise.resolve(a));const u=this._collectAfterHooks();return await fetch(o,a).then(c=>c.json().then(d=>u.reduce(async(k,g)=>g(a,await k),Promise.resolve(d))))}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 n=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"inheriting":return(e=this._source)==null?void 0:e.endpoint;case"default":return n.endpoint}}get fetchGraphQlHeaders(){var h;const e=this._fetchGraphQlHeaders;switch(this._mode){case"standalone":return e;case"inheriting":return{...(h=this._source)==null?void 0:h.fetchGraphQlHeaders,...e};case"default":return{...n.fetchGraphQlHeaders,...e}}}setEndpoint(e){e instanceof H?(this._mode="inheriting",this._source=e):(this._mode="standalone",this._source=e)}_collectBeforeHooks(){return this._mode==="inheriting"?[...this._source._collectBeforeHooks(),...this._beforeHooks]:this._beforeHooks}_collectAfterHooks(){if(this._mode==="inheriting"){const e=this._source._collectAfterHooks();return[...this._afterHooks,...e]}return this._afterHooks}}function v(r){return r=r.replace(/#.*/g,""),r=r.replace(/\s+/g," "),r.trim()}const{setEndpoint:B,setFetchGraphQlHeaders:E,setFetchGraphQlHeader:P,getFetchGraphQlHeader:y,removeFetchGraphQlHeader:C,fetchGraphQl:M,getConfig:O,addBeforeHook:S,addAfterHook:T}=n.getMethods();export{H as FetchGraphQL,T as addAfterHook,S as addBeforeHook,M as fetchGraphQl,O as getConfig,y as getFetchGraphQlHeader,C as removeFetchGraphQlHeader,B as setEndpoint,P as setFetchGraphQlHeader,E as setFetchGraphQlHeaders};
|
|
4
4
|
//# sourceMappingURL=fetch-graphql.js.map
|
package/fetch-graphql.js.map
CHANGED
|
@@ -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 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 * 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 requestInit = await this._beforeHooks.reduce(\n async (prev, hook) => hook(await prev),\n Promise.resolve(requestInit)\n );\n\n return await fetch(url, requestInit).then((r) => r.json().then(\n (response) => this._afterHooks.reduce(\n async (result, hook) => hook(requestInit, await result),\n Promise.resolve(response)\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.\n *\n * @class\n *\n */\nexport class FetchGraphQL extends FetchGraphQLMesh {\n get endpoint() {\n return this._endpoint ?? mesh.endpoint;\n }\n\n get fetchGraphQlHeaders() {\n return (\n (this._endpoint\n ? this._fetchGraphQlHeaders\n : { ...this._fetchGraphQlHeaders, ...mesh.fetchGraphQlHeaders })\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 */\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","r","response","result","mesh","FetchGraphQL","setEndpoint","setFetchGraphQlHeaders","setFetchGraphQlHeader","getFetchGraphQlHeader","removeFetchGraphQlHeader","fetchGraphQl","getConfig","addBeforeHook","addAfterHook"],"mappings":"oKAmCA,MAAMA,EAAiB,CACrB,eAAgB,mBAChB,OAAQ,kBACV,EAEA,MAAMC,CAAiB,CAAvB,cACSC,EAAA,kBAUAA,EAAA,4BAA+B,CAAC,GAEhCA,EAAA,oBAA6B,CAAC,GAE9BA,E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AAQ,GAAG,EAE1BA,EAAM,KAAK,CACpB,CAca,KAAA,CACX,YAAAiB,EACA,uBAAAC,EACA,sBAAAC,EACA,sBAAAC,EACA,yBAAAC,EACA,aAAAC,EACA,UAAAC,EACA,cAAAC,EACA,aAAAC,CACF,EAAIV,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 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 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' | 'inheriting' | '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 'inheriting':\n return (this._source as FetchGraphQL)?.endpoint;\n case 'default':\n return mesh.endpoint;\n }\n }\n\n get fetchGraphQlHeaders(): Header {\n const ownHeaders = this._fetchGraphQlHeaders;\n \n switch (this._mode) {\n case 'standalone':\n // Standalone instance - only use own headers\n return ownHeaders;\n case 'inheriting':\n // Inheriting from parent - merge parent and own headers\n return {\n ...(this._source as FetchGraphQL)?.fetchGraphQlHeaders,\n ...ownHeaders,\n };\n case 'default':\n // Default - inherit from global mesh\n return {\n ...mesh.fetchGraphQlHeaders,\n ...ownHeaders,\n };\n }\n }\n\n /**\n * Sets the GraphQL endpoint or inherits from a parent instance.\n * @param endpoint - The GraphQL endpoint URL string, or a FetchGraphQL instance to inherit from.\n * @example\n * ```js\n * // Set endpoint as string\n * instance.setEndpoint('https://api.example.com/graphql');\n * \n * // Inherit from parent instance\n * const parent = new FetchGraphQL();\n * parent.setEndpoint('https://api.example.com/graphql');\n * \n * const child = new FetchGraphQL();\n * child.setEndpoint(parent); // Inherits endpoint, headers, and hooks\n * ```\n */\n public setEndpoint(endpoint: string | FetchGraphQL): void {\n if (endpoint instanceof FetchGraphQL) {\n // Inherit from parent instance\n this._mode = 'inheriting';\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 * Collects all before hooks from the parent chain.\n * Parent hooks execute first, then child hooks.\n * @protected\n */\n protected _collectBeforeHooks(): BeforeHook[] {\n if (this._mode === 'inheriting') {\n const parentHooks = (this._source as FetchGraphQL)._collectBeforeHooks();\n return [...parentHooks, ...this._beforeHooks];\n }\n return this._beforeHooks;\n }\n \n /**\n * Collects all after hooks from the parent chain.\n * Child hooks execute first, then parent hooks.\n * @protected\n */\n protected _collectAfterHooks(): AfterHook[] {\n if (this._mode === 'inheriting') {\n const parentHooks = (this._source as FetchGraphQL)._collectAfterHooks();\n return [...this._afterHooks, ...parentHooks];\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","ownHeaders","parentHooks","setEndpoint","setFetchGraphQlHeaders","setFetchGraphQlHeader","getFetchGraphQlHeader","removeFetchGraphQlHeader","fetchGraphQl","getConfig","addBeforeHook","addAfterHook"],"mappings":"oKAmCA,MAAMA,EAAiB,CACrB,eAAgB,mBAChB,OAAQ,kBACV,EAEA,MAAMC,CAAiB,CAAvB,cACYC,EAAA,kBAUHA,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,CAkBP,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,KAAK,SAChBO,EA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OAAQ,EAAE,EAGxBA,EAAAA,EAAM,QAAQ,OAAQ,GAAG,EAE1BA,EAAM,KAAK,CACpB,CAgBa,KAAA,CACX,YAAAqB,EACA,uBAAAC,EACA,sBAAAC,EACA,sBAAAC,EACA,yBAAAC,EACA,aAAAC,EACA,UAAAC,EACA,cAAAC,EACA,aAAAC,CACF,EAAIb,EAAK,WAAW"}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name": "@dropins/tools", "version": "1.5.
|
|
1
|
+
{"name": "@dropins/tools", "version": "1.5.2-beta1", "license": "SEE LICENSE IN LICENSE.md"}
|
|
@@ -32,7 +32,7 @@ export type AfterHook<T = any> = (requestInit: RequestInit, response: {
|
|
|
32
32
|
data: T;
|
|
33
33
|
};
|
|
34
34
|
declare class FetchGraphQLMesh {
|
|
35
|
-
_endpoint?: string;
|
|
35
|
+
protected _endpoint?: string;
|
|
36
36
|
get endpoint(): string | undefined;
|
|
37
37
|
get fetchGraphQlHeaders(): Header;
|
|
38
38
|
_fetchGraphQlHeaders: Header;
|
|
@@ -40,7 +40,12 @@ declare class FetchGraphQLMesh {
|
|
|
40
40
|
_afterHooks: AfterHook[];
|
|
41
41
|
/**
|
|
42
42
|
* Sets the GraphQL endpoint.
|
|
43
|
-
* @param endpoint - 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
|
+
* ```
|
|
44
49
|
*/
|
|
45
50
|
setEndpoint(endpoint: string): void;
|
|
46
51
|
/**
|
|
@@ -106,6 +111,16 @@ declare class FetchGraphQLMesh {
|
|
|
106
111
|
* ```
|
|
107
112
|
*/
|
|
108
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[];
|
|
109
124
|
/**
|
|
110
125
|
* Fetches GraphQL data.
|
|
111
126
|
* @param query - The GraphQL query.
|
|
@@ -143,14 +158,45 @@ declare class FetchGraphQLMesh {
|
|
|
143
158
|
}
|
|
144
159
|
/**
|
|
145
160
|
* `FetchGraphQL` is a class that extends `FetchGraphQLMesh`.
|
|
146
|
-
* It provides methods to get the GraphQL endpoint and headers.
|
|
161
|
+
* It provides methods to get the GraphQL endpoint and headers with support for inheritance.
|
|
147
162
|
*
|
|
148
163
|
* @class
|
|
149
164
|
*
|
|
150
165
|
*/
|
|
151
166
|
export declare class FetchGraphQL extends FetchGraphQLMesh {
|
|
167
|
+
private _mode;
|
|
168
|
+
private _source?;
|
|
152
169
|
get endpoint(): string | undefined;
|
|
153
170
|
get fetchGraphQlHeaders(): Header;
|
|
171
|
+
/**
|
|
172
|
+
* Sets the GraphQL endpoint or inherits from a parent instance.
|
|
173
|
+
* @param endpoint - The GraphQL endpoint URL string, or a FetchGraphQL instance to inherit from.
|
|
174
|
+
* @example
|
|
175
|
+
* ```js
|
|
176
|
+
* // Set endpoint as string
|
|
177
|
+
* instance.setEndpoint('https://api.example.com/graphql');
|
|
178
|
+
*
|
|
179
|
+
* // Inherit from parent instance
|
|
180
|
+
* const parent = new FetchGraphQL();
|
|
181
|
+
* parent.setEndpoint('https://api.example.com/graphql');
|
|
182
|
+
*
|
|
183
|
+
* const child = new FetchGraphQL();
|
|
184
|
+
* child.setEndpoint(parent); // Inherits endpoint, headers, and hooks
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
setEndpoint(endpoint: string | FetchGraphQL): void;
|
|
188
|
+
/**
|
|
189
|
+
* Collects all before hooks from the parent chain.
|
|
190
|
+
* Parent hooks execute first, then child hooks.
|
|
191
|
+
* @protected
|
|
192
|
+
*/
|
|
193
|
+
protected _collectBeforeHooks(): BeforeHook[];
|
|
194
|
+
/**
|
|
195
|
+
* Collects all after hooks from the parent chain.
|
|
196
|
+
* Child hooks execute first, then parent hooks.
|
|
197
|
+
* @protected
|
|
198
|
+
*/
|
|
199
|
+
protected _collectAfterHooks(): AfterHook[];
|
|
154
200
|
}
|
|
155
201
|
/**
|
|
156
202
|
* Exports several methods from the `mesh` object.
|
|
@@ -162,6 +208,8 @@ export declare class FetchGraphQL extends FetchGraphQLMesh {
|
|
|
162
208
|
* @property {Function} removeFetchGraphQlHeader - Removes a specific GraphQL header.
|
|
163
209
|
* @property {Function} fetchGraphQl - Fetches GraphQL data.
|
|
164
210
|
* @property {Function} getConfig - Gets the configuration.
|
|
211
|
+
* @property {Function} addBeforeHook - Adds a hook executed before the GraphQL call.
|
|
212
|
+
* @property {Function} addAfterHook - Adds a hook executed after the GraphQL call.
|
|
165
213
|
*/
|
|
166
214
|
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<{
|
|
167
215
|
errors?: FetchQueryError | undefined;
|