@platformatic/client
Advanced tools
Comparing version 0.28.1 to 0.29.0
12
index.js
@@ -41,2 +41,3 @@ 'use strict' | ||
client[kHeaders] = options.headers || {} | ||
const { fullResponse, throwOnError } = options | ||
@@ -50,3 +51,3 @@ for (const path of Object.keys(spec.paths)) { | ||
client[operationId] = buildCallFunction(baseUrl, path, method, methodMeta, options.fullResponse) | ||
client[operationId] = buildCallFunction(baseUrl, path, method, methodMeta, fullResponse, throwOnError) | ||
} | ||
@@ -64,3 +65,3 @@ } | ||
function buildCallFunction (baseUrl, path, method, methodMeta, fullResponse) { | ||
function buildCallFunction (baseUrl, path, method, methodMeta, fullResponse, throwOnError) { | ||
const url = new URL(baseUrl) | ||
@@ -114,3 +115,4 @@ method = method.toUpperCase() | ||
}, | ||
body: JSON.stringify(body) | ||
body: JSON.stringify(body), | ||
throwOnError | ||
}) | ||
@@ -207,2 +209,6 @@ | ||
if (opts.serviceId && !opts.url) { | ||
opts.url = `http://${opts.serviceId}.plt.local` | ||
} | ||
if (opts.type === 'openapi') { | ||
@@ -209,0 +215,0 @@ client = await buildOpenAPIClient(opts) |
{ | ||
"name": "@platformatic/client", | ||
"version": "0.28.1", | ||
"version": "0.29.0", | ||
"description": "A client for all platformatic backends", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,3 @@ 'use strict' | ||
const { test } = require('tap') | ||
const { ResponseStatusCodeError } = require('undici').errors | ||
const { buildServer } = require('../../db') | ||
@@ -85,2 +86,9 @@ const { join } = require('path') | ||
const notFound = await client.getMovieById({ id: 100 }) | ||
same(notFound, { | ||
message: 'Route GET:/movies/100 not found', | ||
error: 'Not Found', | ||
statusCode: 404 | ||
}) | ||
{ | ||
@@ -239,2 +247,19 @@ const movies = await client.getMovies({ 'where.title.eq': 'Star Wars' }) | ||
const notFound = await client.getMovieById({ id: 100 }) | ||
match(notFound, { | ||
statusCode: 404, | ||
headers: { | ||
'content-type': 'application/json; charset=utf-8', | ||
'content-length': '82', | ||
date: matchDate, | ||
connection: 'keep-alive', | ||
'keep-alive': matchKeepAlive | ||
}, | ||
body: { | ||
message: 'Route GET:/movies/100 not found', | ||
error: 'Not Found', | ||
statusCode: 404 | ||
} | ||
}) | ||
{ | ||
@@ -307,2 +332,26 @@ const movies = await client.getMovies({ 'where.title.eq': 'Star Wars' }) | ||
test('throw on error level response', async ({ teardown, rejects }) => { | ||
try { | ||
await fs.unlink(join(__dirname, 'fixtures', 'movies', 'db.sqlite')) | ||
} catch { | ||
// noop | ||
} | ||
const app = await buildServer(join(__dirname, 'fixtures', 'movies', 'platformatic-prefix.db.json')) | ||
teardown(async () => { | ||
await app.close() | ||
}) | ||
await app.start() | ||
const client = await buildOpenAPIClient({ | ||
url: `${app.url}/movies-api/`, | ||
path: join(__dirname, 'fixtures', 'movies', 'openapi.json'), | ||
throwOnError: true | ||
}) | ||
rejects(client.getMovieById({ | ||
id: 100 | ||
}), ResponseStatusCodeError) | ||
}) | ||
test('build basic client from file', async ({ teardown, same, rejects }) => { | ||
@@ -365,2 +414,9 @@ try { | ||
const notFound = await client.getMovieById({ id: 100 }) | ||
same(notFound, { | ||
message: 'Route GET:/movies-api/movies/100 not found', | ||
error: 'Not Found', | ||
statusCode: 404 | ||
}) | ||
{ | ||
@@ -446,2 +502,9 @@ const movies = await client.getMovies({ 'where.title.eq': 'Star Wars' }) | ||
const notFound = await client.getMovieById({ id: 100 }) | ||
same(notFound, { | ||
message: 'Route GET:/movies/100 not found', | ||
error: 'Not Found', | ||
statusCode: 404 | ||
}) | ||
{ | ||
@@ -448,0 +511,0 @@ const movies = await client.getMovies({ 'where.title.eq': 'Star Wars' }) |
@@ -10,2 +10,3 @@ 'use strict' | ||
const Fastify = require('fastify') | ||
const { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('undici') | ||
@@ -340,1 +341,43 @@ test('wrong type', async ({ teardown, same, rejects }) => { | ||
}) | ||
test('serviceId', async ({ teardown, same, rejects }) => { | ||
const agent = getGlobalDispatcher() | ||
teardown(() => { | ||
setGlobalDispatcher(agent) | ||
}) | ||
const mockAgent = new MockAgent() | ||
mockAgent.disableNetConnect() | ||
setGlobalDispatcher(mockAgent) | ||
// Provide the base url to the request | ||
const mockPool = mockAgent.get('http://movies.plt.local') | ||
// intercept the request | ||
mockPool.intercept({ | ||
path: '/movies/', | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
title: 'The Matrix' | ||
}) | ||
}).reply(200, { | ||
id: 1, | ||
title: 'The Matrix' | ||
}) | ||
const app = Fastify() | ||
await app.register(client, { | ||
type: 'openapi', | ||
serviceId: 'movies', | ||
path: join(__dirname, 'fixtures', 'movies', 'openapi.json') | ||
}) | ||
const movie = await app.client.createMovie({ | ||
title: 'The Matrix' | ||
}) | ||
same(movie, { | ||
id: 1, | ||
title: 'The Matrix' | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100211
3612