-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
Documentation/Feature request: Server selection #543
Comments
I don't think this is supported yet 👍 |
I find it natural to specify server variables using export type RequestOpts<T> = {
baseUrl?: string;
fetch?: typeof fetch;
formDataConstructor?: new () => FormData;
headers?: HeadersInit | CustomHeaders;
serverVariables?: T;
} & Omit<RequestInit, "body" | "headers">; Taking the example at the top, { environment?: "api" | "api.dev" | "api.staging" } |
seems feasible. T should default to void. |
I discovered that server variables are taken into consideration when generating the |
I feel like understanding the use-cases here would make sense. Its not really clear to me |
A good overview of uses cases is described here: https://swagger.io/docs/specification/api-host-and-base-path/
At our company we use different environments (dev, staging, prod) and use templating for the api base url (api.service.de, dev.api.service.de, staging.api.service.de, etc). We wrote a thin wrapper around oazapft for this. |
Considering your use case, I thought it would be most desirable to be able to specify server variables via the environment variables. What do you think? |
Let's keep the generated code platform agnostic and not use environment variables. import { defaults, servers } from "./myGeneratedApi.ts";
defaults.baseUrl = servers.prod; the What do you say @florianbepunkt, what would your expected usage of this? Where and when would you expect to set server variables? |
Not sure if variables in the OpenAPI spec are purely scoped to server base urls – this is the only place we are using them. If they are, a function that takes a dict of variables as input and outputs the templated string would make sense to me. Example: const spec = {
servers: [
{
url: "{protocol}://{environment}.your-domain.de/identity",
variables: {
environment: {
default: "api",
enum: ["api", "api.dev", "api.test", "api.staging"],
},
protocol: {
default: "https",
enum: ["https", "http"],
},
},
},
],
}
defaults.baseUrl = await someFnFromOazapft({ protocol: "https://", environment: Promise.resolve("api.test") }); // returns "https://api.test.your-domain.de/identity",
|
This makes sense, thank you for the input.
// myGeneratedApi.ts
/**
* DO NOT MODIFY - This file has been generated using oazapfts.
* See https://www.npmjs.com/package/oazapfts
*/
import { applyServerVariables } from "@oazapfts/runtime/servers";
// ...other code
export const servers = {
server1: "https://petstore.swagger.io/v2",
server2: (
serverVariables: {
protocol: 'https://' | 'http://',
environment: 'prod' | 'staging' | 'test'
}
) => (
applyServerVariables("{protocol}://{environment}.your-domain.de/identity", serverVariables)
)
}; // code using the client
import { defaults, servers } from './myGeneratedApi.ts';
defaults.baseUrl = servers.server2({ protocol: "https://", environment: "prod" }); I'm afraid we'd either only allow sync values here or deal with the promises internally. I would not want to require top level await here. |
Current Oazapfts can already generate that function. So, I believe this issue is about the documentation. |
lol. I wasn't aware of that :) |
So yeah, thanks @bdm-k for pointing this out. This is already working: // code using the client
import { defaults, servers } from './myGeneratedApi.ts';
defaults.baseUrl = servers.server2({ protocol: "https://", environment: "prod" }); |
Does oazapft support server variables? If so how would you use them. If not, this is a feature request.
Example
The text was updated successfully, but these errors were encountered: