Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: send GET request only on first spec fetch #1543

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
});
};

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 @@

// 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 @@
watch: _watch,
}: {
config: Config;
watch?: {
headers: Headers;
lastValue: string | undefined;
};
watch?: WatchValues;
}) => {
const inputPath = config.input.path;

Expand Down
Loading