Skip to content
Draft
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
24 changes: 12 additions & 12 deletions src/code-sample-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,43 +77,43 @@ export function transformPythonCodeSamplesToPython(spec) {
}

/**
* Transforms code samples in an OpenAPI specification to add instance configuration
* for various programming languages. This function adds the necessary instance/server
* Transforms code samples in an OpenAPI specification to add server URL configuration
* for various programming languages. This function adds the necessary server URL
* configuration to existing code samples that only contain API token configuration.
*
* Supported transformations:
* - Python: Adds `instance=os.getenv("GLEAN_INSTANCE", "")` after `api_token`
* - TypeScript: Adds `instance: process.env["GLEAN_INSTANCE"] ?? ""` after `apiToken`
* - Go: Adds `apiclientgo.WithInstance("<value>")` before `WithSecurity`
* - Java: Adds `.instance("<YOUR_GLEAN_INSTANCE_HERE>")` after `.apiToken()`
* - Python: Adds `server_url="mycompany-be.glean.com",` after `api_token`
* - TypeScript: Adds `serverURL: "mycompany-be.glean.com",` after `apiToken`
* - Go: Adds `apiclientgo.WithServerURL("mycompany-be.glean.com"),` before `WithSecurity`
* - Java: Adds `.serverURL("mycompany-be.glean.com")` after `.apiToken()`
*
* @param {Object} spec The OpenAPI specification object containing code samples
* @returns {Object} The modified OpenAPI specification with updated code samples
*/
export function addInstanceToCodeSamples(spec) {
export function addServerURLToCodeSamples(spec) {
const transformationsByLang = {
python: [
[
/([\s]*)(api_token=os\.getenv\("GLEAN_API_TOKEN", ""\),)/,
'$1$2$1instance=os.getenv("GLEAN_INSTANCE", ""),',
'$1$2$1server_url="mycompany-be.glean.com",',
],
],
typescript: [
[
/([\s]*)(apiToken: process\.env\["GLEAN_API_TOKEN"\] \?\? "",)/,
'$1$2$1instance: process.env["GLEAN_INSTANCE"] ?? "",',
'$1$2$1serverURL: "mycompany-be.glean.com",',
],
],
go: [
[
/([\s]*)(apiclientgo\.WithSecurity\(os\.Getenv\("GLEAN_API_TOKEN"\)\),)/,
'$1$2$1apiclientgo.WithInstance(os.Getenv("GLEAN_INSTANCE")),',
'$1$2$1apiclientgo.WithServerURL("mycompany-be.glean.com"),',
],
],
java: [
[
/([\s]*)(\.apiToken\("<YOUR_BEARER_TOKEN_HERE>"\))/,
'$1$2$1.instance("<YOUR_GLEAN_INSTANCE_HERE>")',
'$1$2$1.serverURL("mycompany-be.glean.com")',
],
],
};
Expand Down Expand Up @@ -149,7 +149,7 @@ export function transform(content, _filename) {
const spec = yaml.load(content);

transformPythonCodeSamplesToPython(spec);
addInstanceToCodeSamples(spec);
addServerURLToCodeSamples(spec);

return yaml.dump(spec, {
lineWidth: -1, // Preserve line breaks
Expand Down
12 changes: 6 additions & 6 deletions tests/code-sample-transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ paths:
});
});

describe('addInstanceToCodeSamples', () => {
describe('addServerURLToCodeSamples', () => {
test('updates chat code sample to use namespace', () => {
const spec = yaml.load(fixtureContent);

const updatedSpec = codeSampleTransformer.addInstanceToCodeSamples(spec);
const updatedSpec = codeSampleTransformer.addServerURLToCodeSamples(spec);
const chatSpec = updatedSpec.paths['/rest/api/v1/chat'];

expect(codeSampleTransformer.extractCodeSnippet(chatSpec, 'python'))
Expand All @@ -151,7 +151,7 @@ paths:

with Glean(
api_token=os.getenv("GLEAN_API_TOKEN", ""),
instance=os.getenv("GLEAN_INSTANCE", ""),
server_url="mycompany-be.glean.com",
) as g_client:

res = g_client.client.chat.create(messages=[
Expand All @@ -177,7 +177,7 @@ paths:

const glean = new Glean({
apiToken: process.env["GLEAN_API_TOKEN"] ?? "",
instance: process.env["GLEAN_INSTANCE"] ?? "",
serverURL: "mycompany-be.glean.com",
});

async function run() {
Expand Down Expand Up @@ -220,7 +220,7 @@ paths:

s := apiclientgo.New(
apiclientgo.WithSecurity(os.Getenv("GLEAN_API_TOKEN")),
apiclientgo.WithInstance(os.Getenv("GLEAN_INSTANCE")),
apiclientgo.WithServerURL("mycompany-be.glean.com"),
)

res, err := s.Client.Chat.Create(ctx, components.ChatRequest{
Expand Down Expand Up @@ -262,7 +262,7 @@ paths:

Glean sdk = Glean.builder()
.apiToken("<YOUR_BEARER_TOKEN_HERE>")
.instance("<YOUR_GLEAN_INSTANCE_HERE>")
.serverURL("mycompany-be.glean.com")
.build();

ChatResponse res = sdk.client().chat().create()
Expand Down