Skip to content

Commit

Permalink
fix: send GET request only on first spec fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Jan 9, 2025
1 parent caa46ec commit 7a2d6dc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-crews-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: send GET request only on first spec fetch
104 changes: 53 additions & 51 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,17 @@ const getPlugins = (
});
};

interface WatchValues {
headers: Headers;
lastValue: string | undefined;
}

const getSpec = async ({
inputPath,
watch,
}: {
inputPath: Config['input']['path'];
watch: {
headers: Headers;
lastValue: string | undefined;
};
watch: WatchValues;
}): Promise<
| {
data: JSONSchema;
Expand All @@ -330,59 +332,62 @@ const getSpec = async ({

// no support for watching files and objects for now
if (resolvedInput.type === 'url') {
const request = await sendRequest({
init: {
headers: watch.headers,
method: 'HEAD',
},
url: resolvedInput.path,
});
response = request.response;

if (!response.ok) {
// assume the server is no longer running
// do nothing, it might be restarted later
return {
error: 'not-ok',
response,
};
}
// do not send HEAD request on first run
if (watch.lastValue) {
const request = await sendRequest({
init: {
headers: watch.headers,
method: 'HEAD',
},
url: resolvedInput.path,
});
response = request.response;

if (!response.ok) {
// assume the server is no longer running
// do nothing, it might be restarted later
return {
error: 'not-ok',
response,
};
}

if (response.status === 304) {
return {
error: 'not-modified',
response,
};
}
if (response.status === 304) {
return {
error: 'not-modified',
response,
};
}

if (hasChanged === undefined) {
const eTag = response.headers.get('ETag');
if (eTag) {
hasChanged = eTag !== watch.headers.get('If-None-Match');
if (hasChanged === undefined) {

Check notice

Code scanning / CodeQL

Unneeded defensive code Note

This guard always evaluates to true.
const eTag = response.headers.get('ETag');
if (eTag) {
hasChanged = eTag !== watch.headers.get('If-None-Match');

if (hasChanged) {
watch.headers.set('If-None-Match', eTag);
if (hasChanged) {
watch.headers.set('If-None-Match', eTag);
}
}
}
}

if (hasChanged === undefined) {
const lastModified = response.headers.get('Last-Modified');
if (lastModified) {
hasChanged = lastModified !== watch.headers.get('If-Modified-Since');
if (hasChanged === undefined) {
const lastModified = response.headers.get('Last-Modified');
if (lastModified) {
hasChanged = lastModified !== watch.headers.get('If-Modified-Since');

if (hasChanged) {
watch.headers.set('If-Modified-Since', lastModified);
if (hasChanged) {
watch.headers.set('If-Modified-Since', lastModified);
}
}
}
}

// we definitely know the input has not changed
if (hasChanged === false) {
return {
error: 'not-modified',
response,
};
// we definitely know the input has not changed
if (hasChanged === false) {
return {
error: 'not-modified',
response,
};
}
}

const fileRequest = await sendRequest({
Expand Down Expand Up @@ -578,10 +583,7 @@ export async function createClient(
watch: _watch,
}: {
config: Config;
watch?: {
headers: Headers;
lastValue: string | undefined;
};
watch?: WatchValues;
}) => {
const inputPath = config.input.path;

Expand Down

0 comments on commit 7a2d6dc

Please sign in to comment.