Nestia Fetcher
Outline
npm install --save @nestia/fetcher
@nestia/fetcher
is a fetcher library of Nestia SDK.
When you build an SDK (Software Development Kit) library interacting with remote HTTP server through the Nestia, the SDK library would be dependent on this @nestia/fetcher
. Therefore, if you publish the SDK library on the NPM module, you have to add this @nestia/fetcher
in the dependencies
field of the package.json
.
Also, if you're a client developer who've installed an SDK library which has been generated by the Nestia, you also need to install this @nestia/fetcher
module. With the IConnection
and HttpError
instances provided this @nestia/fetcher
, you can enjoy the SDK library much conveniently.
Example
package.json
When you build an SDK library who've been generated by the Nestia, you have to add this @nestia/fetcher
in the dependencies
field of the package.json
. If your project had installed the Nestia, you can write the dependencies
' property by writing the npx nestia dependencies
command on your console.
{
"name": "payments-server-api",
"dependencies": {
"@nestia/fetcher": "^1.0.0"
}
}
SDK Library
Opening the SDK library source file who've been generated by the Nestia, you can find the SDK library is importing this @nestia/fetcher
module in every functional
files. Therefore, I repeat that you have to put this @nestia/fetcher
down into the dependencies
field of the package.json
.
import { Fetcher, IConnection, Primitive } from "@nestia/fetcher";
export function store
(
connection: IConnection,
input: Primitive<store.Input>
): Promise<store.Output>
{
return Fetcher.fetch
(
connection,
store.CONFIG,
store.METHOD,
store.path(),
input
);
}
export namespace store
{
export type Input = Primitive<IPaymentHistory.IStore>;
export type Output = Primitive<IPaymentHistory>;
export const METHOD = "POST" as const;
export const PATH: string = "/histories";
export const CONFIG: IConnection.IEncrypted = {
input_encrypted: true,
output_encrypted: true,
};
export function path(): string
{
return `/histories`;
}
}
Utilization Code
After you've published the SDK library and let client developers to install the SDK library, the client developers would import this @nestia/fetcher
module, too. They would utilize the IConnection
and HttpError
instances like below.
import payments from "payments-server-api";
import { IPaymentHistory } from "payments-server-api/lib/structures/IPaymentHistory";
import { IConnection, HttpError } from "@nestia/fetcher";
export async function main(): Promise<void>
{
const connection: IConnection = {
host: "http://payments.somewhere.com",
encryption: {
key: "SqwHmmXm1fZteI3URPtoyBWFJDMQ7FBQ",
iv: "9eSfjygAClnE1JJs"
}
};
try
{
const input: IPaymentHistory.IStore = { ...SOME_DATA };
const history: IPaymentHistory = await payments.functional.histories.store
(
connection,
input
);
}
catch (exp)
{
if (exp instanceof HttpError)
console.log(exp);
}
}