ForgeTotal.js 5
nl

Total.js 5

Roep externe diensten aan met API() en de drivers van NEWAPI()

Total.js 5 · total5@0.0.19 apiservicesintegrations Uitgevoerd met Total.js 5

API(name, schema, data, [$]) roept een bewerking van een externe dienst aan en NEWAPI() registreert de driver die de aanroep uitvoert, zodat de code van je acties niet afhangt van hoe de dienst wordt bereikt (HTTP, een bibliotheek, een wachtrij...).

  • NEWAPI('name', driver) registreert de driver van de dienst name ('a,b' registreert één driver voor twee namen en NEWAPI(driver) registreert de default). NEWAPI('name', config, driver) bewaart ook een config-object dat elke aanroep ontvangt als options.config.
  • De driver is function(options, next). options.name is de dienst, options.schema de bewerking, options.data de gegevens en options.config de configuratie. Antwoord met next(error, response).
  • API('name', 'operation', data) geeft een builder terug: sluit hem af met .callback($) (het antwoord gaat naar de actie $), .callback(function(err, response) {}) of await ....promise($). .configure({ key: value }) voegt toe aan options.config voor die aanroep en .error('text') laat een leeg antwoord mislukken met die melding.
  • Als er voor de naam geen driver is geregistreerd, mislukt de aanroep met API is not initialized.
definitions/api.js
// The driver of a fake weather service: in a real one this would be an HTTP request
NEWAPI('weather', { unit: 'C' }, function(options, next) {
	if (options.schema === 'forecast')
		next(null, { city: options.data.city, temperature: 21, unit: options.config.unit, service: options.name });
	else
		next('Unknown operation ' + options.schema);
});
controllers/weather.js
exports.install = function() {
	ROUTE('GET /forecast/', forecast);
	ROUTE('GET /forecast-fahrenheit/', fahrenheit);
	ROUTE('GET /unknown/', unknown);
	ROUTE('GET /missing/', missing);
	ROUTE('GET /promise/', promise);
};

function forecast($) {
	API('weather', 'forecast', { city: $.query.city }).callback($);
}

function fahrenheit($) {
	API('weather', 'forecast', { city: $.query.city }).configure({ unit: 'F' }).callback($);
}

function unknown($) {
	API('weather', 'rain', {}).callback($);
}

function missing($) {
	API('nobody', 'forecast', {}).callback($);
}

async function promise($) {
	const data = await API('weather', 'forecast', { city: 'Oslo' }).promise($);
	$.json({ city: data.city, same: true });
}