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

Configure all api instances at a time #6

Closed
javierbrea opened this issue Jun 7, 2019 · 4 comments · Fixed by #8
Closed

Configure all api instances at a time #6

javierbrea opened this issue Jun 7, 2019 · 4 comments · Fixed by #8
Assignees
Labels
enhancement New feature or request In progress Working on it
Milestone

Comments

@javierbrea
Copy link
Member

When you have to configure many origins at a time with the same value (a very common scenario for "baseUrl", or authentication headers, for example), you have to import all desired origins and iterate over all calling to its "config" method.
This is really annoying because for each origin you use, you have to add the new dependency to your "configurator" piece, and you have to repeat this pattern in every applications.

It would be very useful to export a method for configuring or cleaning groups of Api instances at a time. (Maybe a system based on tags will be very flexible to allow configuring or cleaning only certain subgroups).

A proposal:

import { Api } from "@xbyorange/mercury-api";

export const fooCollection = new Api("/foo-url", {
  tags: ["xbyorange-api",  "need-auth"]
})

export const fooPublicCollection = new Api("/foo-public-url", {
  tags: ["xbyorange-api"]
})

export const fooExternalApi  = new Api("/foo-url", {
  tags: ["external-api"]
})

Now, you can configure each api url separately:

import { apis } from "@xbyorange/mercury-api"

apis.config("xbyorange-api", {
  baseUrl: "https://foo-url/api"
})

apis.config("external-api", {
  baseUrl: "https://foo-external-domain"
})

To configure authentication:

import { apis } from "@xbyorange/mercury-api"

export const setAuth = token => {
  apis.setHeaders("need-auth", {
    "Authorization": `Bearer ${token}`
  })
}
@javierbrea javierbrea added the enhancement New feature or request label Jun 7, 2019
@javierbrea javierbrea self-assigned this Jun 7, 2019
@javierbrea
Copy link
Member Author

Another instance methods, such as clean and setHeaders should be exposed too to this utility:

import { apis } from "@xbyorange/mercury-api"
apis.clean("xbyorange-api")

@manuelitox
Copy link

Sounds good!

Another option could be:

import { Api } from "@xbyorange/mercury-api";

const catalog = new Api('/catalog-api')
catalog.tags = ['external-api', 'internal-api']

I think this way could be more friendly when reading it.

What do you think?

@javierbrea
Copy link
Member Author

@manuelitox I think it can be good idea to expose setters for configuration, if you prefer that syntax, but I think that then we should implement setters for all configuration properties, not only for the tags one.

Maybe it is better to open another issue related to this. Feel free if you agree ;)

@javierbrea javierbrea added the In progress Working on it label Jun 7, 2019
@manuelitox
Copy link

Done!

Issue: Implement setters for all configurations and HTTP's request methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request In progress Working on it
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants