@camptocamp/ogc-client
Advanced tools
Comparing version 1.1.1-dev.ddbb5b0 to 1.1.1-dev.e9b434a
import { EndpointError } from "../shared/errors.js"; | ||
import { getFetchOptions } from "../shared/http-utils.js"; | ||
import { sharedFetch } from "../shared/http-utils.js"; | ||
function fetchDocument(url) { | ||
const urlObj = new URL(url, window.location.toString()); | ||
urlObj.searchParams.set("f", "json"); | ||
const options = getFetchOptions(); | ||
const optionsHeaders = "headers" in options ? options.headers : {}; | ||
return fetch(urlObj.toString(), { | ||
...options, | ||
headers: { ...optionsHeaders, Accept: "application/json" } | ||
}).then((resp) => { | ||
return sharedFetch(urlObj.toString(), "GET", true).then((resp) => { | ||
if (!resp.ok) { | ||
@@ -13,0 +8,0 @@ throw new Error(`The document at ${urlObj} could not be fetched.`); |
@@ -21,3 +21,3 @@ import { FetchOptions } from './models.js'; | ||
*/ | ||
export declare function sharedFetch(url: string, method?: 'GET' | 'HEAD'): Promise<any>; | ||
export declare function sharedFetch(url: string, method?: 'GET' | 'HEAD', asJson?: boolean): Promise<any>; | ||
/** | ||
@@ -24,0 +24,0 @@ * Runs a GET HTTP request to the provided URL and resolves to the |
@@ -23,8 +23,17 @@ import { parseXmlString } from "./xml-utils.js"; | ||
} | ||
function sharedFetch(url, method = "GET") { | ||
const fetchKey = `${method}#${url}`; | ||
function sharedFetch(url, method = "GET", asJson) { | ||
let fetchKey = `${method}#${url}`; | ||
if (asJson) { | ||
fetchKey = `${method}#asJson#${url}`; | ||
} | ||
if (fetchPromises.has(fetchKey)) { | ||
return fetchPromises.get(fetchKey); | ||
} | ||
const promise = fetch(url, { ...getFetchOptions(), method }).catch((e) => e).then((resp) => { | ||
const options = { ...getFetchOptions() }; | ||
options.method = method; | ||
if (asJson) { | ||
options.headers = "headers" in options ? options.headers : {}; | ||
options.headers["Accept"] = "application/json"; | ||
} | ||
const promise = fetch(url, options).catch((e) => e).then((resp) => { | ||
fetchPromises.delete(fetchKey); | ||
@@ -31,0 +40,0 @@ return resp; |
{ | ||
"name": "@camptocamp/ogc-client", | ||
"version": "1.1.1-dev.ddbb5b0", | ||
"version": "1.1.1-dev.e9b434a", | ||
"description": "A pure JS library for interacting with geospatial services.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/dist-node.js", |
import { OgcApiDocument, OgcApiDocumentLink } from './model.js'; | ||
import { EndpointError } from '../shared/errors.js'; | ||
import { getFetchOptions } from '../shared/http-utils.js'; | ||
import { sharedFetch } from '../shared/http-utils.js'; | ||
@@ -10,8 +10,3 @@ export function fetchDocument<T extends OgcApiDocument>( | ||
urlObj.searchParams.set('f', 'json'); | ||
const options = getFetchOptions(); | ||
const optionsHeaders = 'headers' in options ? options.headers : {}; | ||
return fetch(urlObj.toString(), { | ||
...options, | ||
headers: { ...optionsHeaders, Accept: 'application/json' }, | ||
}).then((resp) => { | ||
return sharedFetch(urlObj.toString(), 'GET', true).then((resp) => { | ||
if (!resp.ok) { | ||
@@ -18,0 +13,0 @@ throw new Error(`The document at ${urlObj} could not be fetched.`); |
@@ -410,2 +410,3 @@ import { EndpointError } from './errors.js'; | ||
...sampleOptions, | ||
method: 'GET', | ||
headers: { | ||
@@ -412,0 +413,0 @@ ...sampleOptions.headers, |
@@ -46,10 +46,23 @@ import { parseXmlString } from './xml-utils.js'; | ||
*/ | ||
export function sharedFetch(url: string, method: 'GET' | 'HEAD' = 'GET') { | ||
const fetchKey = `${method}#${url}`; | ||
export function sharedFetch( | ||
url: string, | ||
method: 'GET' | 'HEAD' = 'GET', | ||
asJson?: boolean | ||
) { | ||
let fetchKey = `${method}#${url}`; | ||
if (asJson) { | ||
fetchKey = `${method}#asJson#${url}`; | ||
} | ||
if (fetchPromises.has(fetchKey)) { | ||
return fetchPromises.get(fetchKey); | ||
} | ||
const options: RequestInit = { ...getFetchOptions() }; | ||
options.method = method; | ||
if (asJson) { | ||
options.headers = 'headers' in options ? options.headers : {}; | ||
options.headers['Accept'] = 'application/json'; | ||
} | ||
// to avoid unhandled promise rejections this promise will never reject, | ||
// but only return errors as a normal value | ||
const promise = fetch(url, { ...getFetchOptions(), method }) | ||
const promise = fetch(url, options) | ||
.catch((e) => e) | ||
@@ -56,0 +69,0 @@ .then((resp) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
978397
15349
2