samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.85k stars 95 forks source link

Set SDK base URL #396

Closed DavidVaness closed 1 year ago

DavidVaness commented 1 year ago

Question

Hey @samchon, thank you for this great library. We are just integrating it into our project and are wondering if there is a more ergonomic way to set up the baseUrl for the sdk client. We would like to set it up once and then change it depending on the environment.

maybe similar to the way axios does it?

// Set config defaults when creating the instance
const instance = axios.create({
  baseURL: 'https://api.example.com'
});

Cheers David

samchon commented 1 year ago

What about making a global function or utility class which follows same interface with IConnection?

export function prepare(): api.IConnection;

export class MyConnection implements api.IConnection {
}
DavidVaness commented 1 year ago

Sounds good to me, will have a look at this tomorrow. Ty

DavidVaness commented 1 year ago

Okay, so I misunderstood your response.
This would still mean I have to add a connection to every request I have to make. For sure we could abstract away the logic of setting up the connection like you showed above, but the argument has to get passed for every request.

My wish would be to something like this:

import api from '@myOrg/my-api';

export const instance = api.create({
  connection: import.meta.env.VITE_BACKEND_URL
});

and every request has the connection already set up

import { instance } from './api';

const data = await instance.functional.domain.endpoint();
samchon commented 1 year ago

No plan to make the connection type to be such OOP styled.

My plan is to make IConnection to be a common interface for every SDK.