-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix): add docs cache invalidation endpoint (#854)
- Loading branch information
Showing
30 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
imports: | ||
commons: commons.yml | ||
|
||
service: | ||
base-path: /docs-cache | ||
auth: true | ||
endpoints: | ||
invalidate: | ||
method: POST | ||
path: /invalidate | ||
request: | ||
name: InvalidateCachedDocsRequest | ||
body: | ||
properties: | ||
url: string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/fdr-sdk/src/client/generated/api/resources/docsCache/client/Client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
|
||
import * as environments from "../../../../environments"; | ||
import * as core from "../../../../core"; | ||
import * as FernRegistry from "../../.."; | ||
import urlJoin from "url-join"; | ||
|
||
export declare namespace DocsCache { | ||
interface Options { | ||
environment?: core.Supplier<environments.FernRegistryEnvironment | string>; | ||
token?: core.Supplier<core.BearerToken | undefined>; | ||
} | ||
|
||
interface RequestOptions { | ||
timeoutInSeconds?: number; | ||
maxRetries?: number; | ||
} | ||
} | ||
|
||
export class DocsCache { | ||
constructor(protected readonly _options: DocsCache.Options = {}) {} | ||
|
||
public async invalidate( | ||
request: FernRegistry.InvalidateCachedDocsRequest, | ||
requestOptions?: DocsCache.RequestOptions | ||
): Promise<core.APIResponse<void, FernRegistry.docsCache.invalidate.Error>> { | ||
const _response = await core.fetcher({ | ||
url: urlJoin( | ||
(await core.Supplier.get(this._options.environment)) ?? environments.FernRegistryEnvironment.Dev, | ||
"/docs-cache/invalidate" | ||
), | ||
method: "POST", | ||
headers: { | ||
Authorization: await this._getAuthorizationHeader(), | ||
"X-Fern-Language": "JavaScript", | ||
"X-Fern-Runtime": core.RUNTIME.type, | ||
"X-Fern-Runtime-Version": core.RUNTIME.version, | ||
}, | ||
contentType: "application/json", | ||
body: request, | ||
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : undefined, | ||
maxRetries: requestOptions?.maxRetries, | ||
}); | ||
if (_response.ok) { | ||
return { | ||
ok: true, | ||
body: undefined, | ||
}; | ||
} | ||
|
||
return { | ||
ok: false, | ||
error: FernRegistry.docsCache.invalidate.Error._unknown(_response.error), | ||
}; | ||
} | ||
|
||
protected async _getAuthorizationHeader() { | ||
const bearer = await core.Supplier.get(this._options.token); | ||
if (bearer != null) { | ||
return `Bearer ${bearer}`; | ||
} | ||
|
||
return undefined; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/fdr-sdk/src/client/generated/api/resources/docsCache/client/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./requests"; | ||
export * as invalidate from "./invalidate"; |
38 changes: 38 additions & 0 deletions
38
packages/fdr-sdk/src/client/generated/api/resources/docsCache/client/invalidate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
|
||
import * as FernRegistry from "../../.."; | ||
import * as core from "../../../../core"; | ||
|
||
export type Error = FernRegistry.docsCache.invalidate.Error._Unknown; | ||
|
||
export declare namespace Error { | ||
interface _Unknown { | ||
error: void; | ||
content: core.Fetcher.Error; | ||
} | ||
|
||
interface _Visitor<_Result> { | ||
_other: (value: core.Fetcher.Error) => _Result; | ||
} | ||
} | ||
|
||
export const Error = { | ||
_unknown: (fetcherError: core.Fetcher.Error): FernRegistry.docsCache.invalidate.Error._Unknown => { | ||
return { | ||
error: undefined, | ||
content: fetcherError, | ||
}; | ||
}, | ||
|
||
_visit: <_Result>( | ||
value: FernRegistry.docsCache.invalidate.Error, | ||
visitor: FernRegistry.docsCache.invalidate.Error._Visitor<_Result> | ||
): _Result => { | ||
switch (value.error) { | ||
default: | ||
return visitor._other(value as any); | ||
} | ||
}, | ||
} as const; |
7 changes: 7 additions & 0 deletions
7
...c/client/generated/api/resources/docsCache/client/requests/InvalidateCachedDocsRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
|
||
export interface InvalidateCachedDocsRequest { | ||
url: string; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/fdr-sdk/src/client/generated/api/resources/docsCache/client/requests/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { InvalidateCachedDocsRequest } from "./InvalidateCachedDocsRequest"; |
1 change: 1 addition & 0 deletions
1
packages/fdr-sdk/src/client/generated/api/resources/docsCache/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./client"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { DocsV1Write } from "@fern-api/fdr-sdk"; | ||
import { inject } from "vitest"; | ||
import { getAPIResponse, getClient } from "../util"; | ||
|
||
export const WRITE_DOCS_REGISTER_DEFINITION: DocsV1Write.DocsDefinition = { | ||
pages: {}, | ||
config: { | ||
navigation: { | ||
items: [], | ||
}, | ||
}, | ||
}; | ||
|
||
it("docs invalidate cache", async () => { | ||
const fdr = getClient({ authed: true, url: inject("url") }); | ||
const domain = `docs-${Math.random()}.fern.com`; | ||
|
||
// register docs | ||
const startDocsRegisterResponse = getAPIResponse( | ||
await fdr.docs.v1.write.startDocsRegister({ | ||
orgId: "fern", | ||
domain, | ||
filepaths: ["logo.png", "guides/guide.mdx"], | ||
}), | ||
); | ||
await fdr.docs.v1.write.finishDocsRegister(startDocsRegisterResponse.docsRegistrationId, { | ||
docsDefinition: WRITE_DOCS_REGISTER_DEFINITION, | ||
}); | ||
|
||
const response = await fdr.docsCache.invalidate({ | ||
url: `https://${domain}`, | ||
}); | ||
|
||
expect(response.ok).toEqual(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
servers/fdr/src/api/generated/api/resources/docsCache/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./service"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./service"; |
19 changes: 19 additions & 0 deletions
19
servers/fdr/src/api/generated/api/resources/docsCache/service/DocsCacheService.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
import * as FernRegistry from "../../.."; | ||
import express from "express"; | ||
export interface DocsCacheServiceMethods { | ||
invalidate(req: express.Request<never, never, FernRegistry.InvalidateCachedDocsRequest, never>, res: { | ||
send: () => Promise<void>; | ||
cookie: (cookie: string, value: string, options?: express.CookieOptions) => void; | ||
locals: any; | ||
}): void | Promise<void>; | ||
} | ||
export declare class DocsCacheService { | ||
private readonly methods; | ||
private router; | ||
constructor(methods: DocsCacheServiceMethods, middleware?: express.RequestHandler[]); | ||
addMiddleware(handler: express.RequestHandler): this; | ||
toRouter(): express.Router; | ||
} |
54 changes: 54 additions & 0 deletions
54
servers/fdr/src/api/generated/api/resources/docsCache/service/DocsCacheService.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import express from "express"; | ||
import * as errors from "../../../../errors"; | ||
export class DocsCacheService { | ||
constructor(methods, middleware = []) { | ||
this.methods = methods; | ||
this.router = express.Router({ mergeParams: true }).use(express.json({ | ||
strict: false, | ||
}), ...middleware); | ||
} | ||
addMiddleware(handler) { | ||
this.router.use(handler); | ||
return this; | ||
} | ||
toRouter() { | ||
this.router.post("/invalidate", (req, res, next) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
yield this.methods.invalidate(req, { | ||
send: () => __awaiter(this, void 0, void 0, function* () { | ||
res.sendStatus(204); | ||
}), | ||
cookie: res.cookie.bind(res), | ||
locals: res.locals, | ||
}); | ||
next(); | ||
} | ||
catch (error) { | ||
console.error(error); | ||
if (error instanceof errors.FernRegistryError) { | ||
console.warn(`Endpoint 'invalidate' unexpectedly threw ${error.constructor.name}.` + | ||
` If this was intentional, please add ${error.constructor.name} to` + | ||
" the endpoint's errors list in your Fern Definition."); | ||
yield error.send(res); | ||
} | ||
else { | ||
res.status(500).json("Internal Server Error"); | ||
} | ||
next(error); | ||
} | ||
})); | ||
return this.router; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
servers/fdr/src/api/generated/api/resources/docsCache/service/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./requests"; |
1 change: 1 addition & 0 deletions
1
servers/fdr/src/api/generated/api/resources/docsCache/service/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./requests"; |
6 changes: 6 additions & 0 deletions
6
...c/api/generated/api/resources/docsCache/service/requests/InvalidateCachedDocsRequest.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
export interface InvalidateCachedDocsRequest { | ||
url: string; | ||
} |
4 changes: 4 additions & 0 deletions
4
...src/api/generated/api/resources/docsCache/service/requests/InvalidateCachedDocsRequest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* This file was auto-generated by Fern from our API Definition. | ||
*/ | ||
export {}; |
1 change: 1 addition & 0 deletions
1
servers/fdr/src/api/generated/api/resources/docsCache/service/requests/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { InvalidateCachedDocsRequest } from "./InvalidateCachedDocsRequest"; |
1 change: 1 addition & 0 deletions
1
servers/fdr/src/api/generated/api/resources/docsCache/service/requests/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
servers/fdr/src/controllers/docs-cache/getDocsCacheService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { DocsCacheService } from "../../api/generated/api/resources/docsCache/service/DocsCacheService"; | ||
import { type FdrApplication } from "../../app"; | ||
import { ParsedBaseUrl } from "../../util/ParsedBaseUrl"; | ||
|
||
export function getDocsCacheService(app: FdrApplication): DocsCacheService { | ||
return new DocsCacheService({ | ||
invalidate: async (req, res) => { | ||
await app.docsDefinitionCache.invalidateCache(ParsedBaseUrl.parse(req.body.url).toURL()); | ||
return res.send(); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.