@nxtdtc/apiclient v1.5.0
API Client Library
What Am I?
This package standardizes communication with our micro services and exposes common endpoints through public methods (e.g apiClient.cart.addToCart(...)
)
To initialize a new apiClient you must provide a url
, user
, pass
, replyTo
, and an optional config
object to override the default.
ApiClient will establish a connection over websocket protocol to the url provided and maintain the replyTo
id in closure scope.
Once the client has connected, it can subscribe to a channel to listen for incoming messages. The subscribe
method accepts a callback as the second argument which can be passed in from the caller to do anything it wants with the incoming messages.
Example:
// create a new instance of ApiClient
const apiClient = ApiClient({
url: process.env.REACT_APP_CLOUDAMQP_HOSTNAME,
user: process.env.REACT_APP_CLOUDAMQP_USERNAME,
pass: process.env.REACT_APP_CLOUDAMQP_PASSWORD,
replyTo: ObjectID()
});
// wait for successful connection or to throw an error
await apiClient.connectClient();
// subscribe to a queue and do something with message
apiClient.subscribe(`/queue/${replyTo}`, (message) => console.log(message));
// make a request
apiClient.bootstrap.getBootstrap({
token: olympusToken,
body: { cartId: 'cartId' }
});
Development
You will need to use npm link
in order to use this library while developing against other projects that use it as a dependency. For example, if a storefront uses this library:
In this library terminal run npm link
&& npm start
In the storefront terminal run npm link @nxtdtc/apiclient
UI Client
Because this small library will be used with another app as a consumer (usually something front-end-y) there is a small app within this project that exposes all the methods with an editor to create a payload to send our microservices. Responses from our microservices will appear in the editor to the right.
To start the UI client: npm run start:client
&& open up http://localhost:8080/
Deployment
This is an NPM package. Any breaking changes should be discussed with the team and be released as major
version updates
Most updates will be minor
.
patch
updates should be reserved for hotfixes if necessary. For example, if a storefront is using a minor version v1.1 that is broken - and the current version that is published is v1.3 you will want to check out the library at v1.1 - create a fix and release a patch version v1.1.1
To deploy this library for consumption:
npm version minor | major | patch
(pssst: you will usually use npm version minor
)
npm publish
FAQs
Why don't I just use postman to test?
- postman will not encode our messages to binary format - you COULD do this but it can prove to be challenging
Why do we encode our messages?
- Our microservices use RabbitMQ as a message broker and we communicate via websockets so we need to send messages in the format they expect and we use msgpack for this
What is replyTo
used for?
replyTo
is a unique id we use to maintain a reference to what message queue we are subscribing to. For example, we don't wantuser123
to get messages meant foruser456
- our microservices will send a response meant for the specificreplyTo
id we send in our request
How do I add my endpoint to the UI client?
- You're in luck my friend - your method will be automatically picked up by the UI client and be able to test by adding a request body
How can I test my endpoint?
- You can modify the url, pass, connection string and store code in the selectors on the client. For params, you should add an object with all the information your endpoint expects
{
cartId: '123';
}