@faststore/graphql-utils
Advanced tools
Comparing version 2.2.78 to 3.0.0
export declare const gql: (_: TemplateStringsArray) => never; | ||
export interface GraphQLResponse<D = any> { | ||
data: D; | ||
errors: any[]; | ||
} | ||
export interface RequestOptions<V = any> { | ||
operationName: string; | ||
variables: V; | ||
fetchOptions?: RequestInit; | ||
} | ||
export declare const request: <V = any, D = any>(endpoint: string, { operationName, variables, fetchOptions }: RequestOptions<V>) => Promise<GraphQLResponse<D>>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.request = exports.gql = void 0; | ||
exports.gql = void 0; | ||
const gql = (_) => { | ||
throw new Error(`[graphql-utils]: This should have been removed by the babel plugin. Please make sure the babel plugin is configured correctly`); | ||
throw new Error(`[graphql-utils]: Depreciation notice: the gql function should be imported from @faststore/core. Follow this migration guide: faststore.dev/docs/migration/graphql-utils`); | ||
}; | ||
exports.gql = gql; | ||
const DEFAULT_HEADERS_BY_VERB = { | ||
POST: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
const request = async (endpoint, { operationName, variables, fetchOptions }) => { | ||
// Uses method from fetchOptions. | ||
// If no one is passed, figure out with via heuristic | ||
const method = fetchOptions?.method !== undefined | ||
? fetchOptions.method.toUpperCase() | ||
: operationName.endsWith('Query') | ||
? 'GET' | ||
: 'POST'; | ||
const params = new URLSearchParams({ | ||
operationName, | ||
...(method === 'GET' && { variables: JSON.stringify(variables) }), | ||
}); | ||
const body = method === 'POST' | ||
? JSON.stringify({ | ||
operationName, | ||
variables, | ||
}) | ||
: undefined; | ||
const url = `${endpoint}?${params.toString()}`; | ||
const response = await fetch(url, { | ||
method, | ||
body, | ||
...fetchOptions, | ||
headers: { | ||
...DEFAULT_HEADERS_BY_VERB[method], | ||
...fetchOptions?.headers, | ||
}, | ||
}); | ||
return response.json(); | ||
}; | ||
exports.request = request; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@faststore/graphql-utils", | ||
"version": "2.2.78", | ||
"version": "3.0.0", | ||
"description": "GraphQL utilities", | ||
@@ -24,4 +24,4 @@ "repository": { | ||
"devDependencies": { | ||
"@faststore/eslint-config": "^2.2.78", | ||
"@faststore/shared": "^2.2.78", | ||
"@faststore/eslint-config": "^3.0.0", | ||
"@faststore/shared": "^3.0.0", | ||
"cross-env": "^7.0.2", | ||
@@ -37,3 +37,3 @@ "eslint": "7.32.0", | ||
}, | ||
"gitHead": "3c62fe1c0b2432c97a93a2be93f4c761d0ed546c" | ||
"gitHead": "5352d481c0020af1d2f7e7286775fd5ad30ebcef" | ||
} |
export const gql = (_: TemplateStringsArray) => { | ||
throw new Error( | ||
`[graphql-utils]: This should have been removed by the babel plugin. Please make sure the babel plugin is configured correctly` | ||
`[graphql-utils]: Depreciation notice: the gql function should be imported from @faststore/core. Follow this migration guide: faststore.dev/docs/migration/graphql-utils` | ||
) | ||
} | ||
export interface GraphQLResponse<D = any> { | ||
data: D | ||
errors: any[] | ||
} | ||
export interface RequestOptions<V = any> { | ||
operationName: string | ||
variables: V | ||
fetchOptions?: RequestInit | ||
} | ||
const DEFAULT_HEADERS_BY_VERB: Record<string, Record<string, string>> = { | ||
POST: { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
export const request = async <V = any, D = any>( | ||
endpoint: string, | ||
{ operationName, variables, fetchOptions }: RequestOptions<V> | ||
): Promise<GraphQLResponse<D>> => { | ||
// Uses method from fetchOptions. | ||
// If no one is passed, figure out with via heuristic | ||
const method = | ||
fetchOptions?.method !== undefined | ||
? fetchOptions.method.toUpperCase() | ||
: operationName.endsWith('Query') | ||
? 'GET' | ||
: 'POST' | ||
const params = new URLSearchParams({ | ||
operationName, | ||
...(method === 'GET' && { variables: JSON.stringify(variables) }), | ||
}) | ||
const body = | ||
method === 'POST' | ||
? JSON.stringify({ | ||
operationName, | ||
variables, | ||
}) | ||
: undefined | ||
const url = `${endpoint}?${params.toString()}` | ||
const response = await fetch(url, { | ||
method, | ||
body, | ||
...fetchOptions, | ||
headers: { | ||
...DEFAULT_HEADERS_BY_VERB[method], | ||
...fetchOptions?.headers, | ||
}, | ||
}) | ||
return response.json() | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
48227
11
20