@platformatic/client
Advanced tools
Comparing version 0.20.1 to 0.21.0
21
index.js
'use strict' | ||
const { request } = require('undici') | ||
const { join } = require('path') | ||
const fs = require('fs/promises') | ||
@@ -25,2 +26,3 @@ const kHeaders = Symbol('headers') | ||
let spec | ||
let baseUrl | ||
@@ -30,5 +32,7 @@ // this is tested, not sure why c8 is not picking it up | ||
spec = JSON.parse(await fs.readFile(options.path, 'utf8')) | ||
baseUrl = options.url.replace(/\/$/, '') | ||
} else if (options.url) { | ||
const res = await request(options.url) | ||
spec = await res.body.json() | ||
baseUrl = computeURLWithoutPath(options.url) | ||
} else { | ||
@@ -38,3 +42,2 @@ throw new Error('options.url or options.file are required') | ||
const baseUrl = computeURLWithoutPath(options.url) | ||
client[kHeaders] = options.headers || {} | ||
@@ -65,5 +68,7 @@ | ||
method = method.toUpperCase() | ||
path = join(url.pathname, path) | ||
const pathParams = methodMeta.parameters?.filter(p => p.in === 'path') || [] | ||
const queryParams = methodMeta.parameters?.filter(p => p.in === 'query') || [] | ||
const headerParams = methodMeta.parameters?.filter(p => p.in === 'header') || [] | ||
@@ -94,2 +99,9 @@ return async function (args) { | ||
for (const param of headerParams) { | ||
if (body[param.name] !== undefined) { | ||
headers[param.name] = body[param.name] | ||
body[param.name] = undefined | ||
} | ||
} | ||
urlToCall.search = query.toString() | ||
@@ -104,4 +116,9 @@ urlToCall.pathname = pathToCall | ||
}, | ||
body: JSON.stringify(args) | ||
body: JSON.stringify(body) | ||
}) | ||
if (res.statusCode === 204) { | ||
return await res.body.dump() | ||
} | ||
return await res.body.json() | ||
@@ -108,0 +125,0 @@ } |
{ | ||
"name": "@platformatic/client", | ||
"version": "0.20.1", | ||
"version": "0.21.0", | ||
"description": "A client for all platformatic backends", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -684,4 +684,23 @@ { | ||
} | ||
}, | ||
"/hello/header/name": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
"schema": { | ||
"type": "string" | ||
}, | ||
"in": "header", | ||
"name": "name", | ||
"required": true | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "Default Response" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
@@ -22,2 +22,16 @@ 'use strict' | ||
}) | ||
app.get('/hello/header/name', { | ||
schema: { | ||
headers: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' } | ||
}, | ||
required: ['name'] | ||
} | ||
} | ||
}, async (request, reply) => { | ||
return { hello: request.headers.name } | ||
}) | ||
} |
@@ -684,4 +684,31 @@ { | ||
} | ||
}, | ||
"/hello/header/name": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
"schema": { | ||
"type": "string" | ||
}, | ||
"in": "header", | ||
"name": "name", | ||
"required": true | ||
}, | ||
{ | ||
"schema": { | ||
"type": "string" | ||
}, | ||
"in": "header", | ||
"name": "id", | ||
"required": false | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "Default Response" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
@@ -5,2 +5,30 @@ 'use strict' | ||
module.exports = async function (app) { | ||
app.put('/movies/:id/:title', { | ||
schema: { | ||
operationId: 'updateMovieTitle', | ||
params: { | ||
type: 'object', | ||
properties: { | ||
id: { type: 'string' }, | ||
title: { type: 'string' } | ||
}, | ||
required: ['id', 'title'] | ||
}, | ||
responses: { | ||
204: { | ||
description: 'Successuly updated title' | ||
} | ||
} | ||
} | ||
}, async (request, reply) => { | ||
await app.platformatic.entities.movie.save({ | ||
fields: ['id', 'title'], | ||
input: { | ||
id: request.params.id, | ||
title: request.params.title | ||
} | ||
}) | ||
reply.status(204) | ||
}) | ||
app.get('/hello', async (request, reply) => { | ||
@@ -24,2 +52,17 @@ return { hello: 'world' } | ||
app.get('/hello/header/name', { | ||
schema: { | ||
headers: { | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
id: { type: 'string' } | ||
}, | ||
required: ['name'] | ||
} | ||
} | ||
}, async (request, reply) => { | ||
return { hello: request.headers.name } | ||
}) | ||
app.post('/weird/:name', { | ||
@@ -26,0 +69,0 @@ schema: { |
@@ -66,2 +66,15 @@ 'use strict' | ||
const updatedTitle = await client.updateMovieTitle({ id: 1, title: 'The Matrix Revolutions' }) | ||
same(updatedTitle, undefined) | ||
const movie3 = await client.getMovieById({ | ||
id: 1 | ||
}) | ||
same(movie3, { | ||
id: 1, | ||
title: 'The Matrix Revolutions' | ||
}) | ||
await rejects(client.getMovieById()) | ||
@@ -83,2 +96,7 @@ | ||
} | ||
{ | ||
const hello = await client.getHelloHeaderName({ name: 'Matteo' }) | ||
same(hello, { hello: 'Matteo' }) | ||
} | ||
}) | ||
@@ -92,3 +110,3 @@ | ||
} | ||
const server = await buildServer(join(__dirname, 'fixtures', 'movies', 'platformatic.db.json')) | ||
const server = await buildServer(join(__dirname, 'fixtures', 'movies', 'platformatic-prefix.db.json')) | ||
teardown(server.stop) | ||
@@ -98,3 +116,3 @@ await server.listen() | ||
const client = await buildOpenAPIClient({ | ||
url: server.url, | ||
url: `${server.url}/movies-api/`, | ||
path: join(__dirname, 'fixtures', 'movies', 'openapi.json') | ||
@@ -156,2 +174,7 @@ }) | ||
} | ||
{ | ||
const hello = await client.getHelloHeaderName({ name: 'Matteo' }) | ||
same(hello, { hello: 'Matteo' }) | ||
} | ||
}) | ||
@@ -229,2 +252,7 @@ | ||
} | ||
{ | ||
const hello = await client.getHelloHeaderName({ name: 'Matteo' }) | ||
same(hello, { hello: 'Matteo' }) | ||
} | ||
}) |
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
92444
27
3300