Skip to content

Commit 79baefe

Browse files
committed
Support OpenAI plugins as first-class citizens
1 parent 90df66c commit 79baefe

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-6
lines changed

lib/gateway.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ class Gateway extends EventEmitter {
216216
__wellKnownResponse__ (req, res, body, headers) {
217217
headers = headers || {};
218218
headers = this.__createHeaders__(req, headers);
219-
headers['content-type'] = 'application/json';
220219
return this.__endRequest__(
221220
200,
222221
this.__formatHeaders__(headers),
@@ -1731,7 +1730,7 @@ class Gateway extends EventEmitter {
17311730
data.pluginAuthorized = true;
17321731
data.plugin = {};
17331732
data.server = 'FunctionScript Gateway';
1734-
data.origin = urlinfo.origin;
1733+
data.origin = urlinfo.origin || 'localhost';
17351734
data.identifier = 'service.localhost';
17361735
return callback(null, definition, data, buffer);
17371736
}

lib/well-knowns.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ const wellKnowns = {
423423

424424
return {
425425
headers: {
426-
'Content-Type': 'application/yaml'
426+
'Content-Type': 'application/json'
427427
},
428428
body
429429
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "functionscript",
3-
"version": "2.9.2",
3+
"version": "2.10.0",
44
"description": "An API gateway and framework for turning functions into web services",
55
"author": "Keith Horwood <keithwhor@gmail.com>",
66
"main": "index.js",

tests/gateway/functions/my_function.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* My function
23
* @returns {number}
34
*/
45
module.exports = (a = 1, b = 2, c = 3, callback) => {

tests/gateway/tests.js

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function parseServerSentEvents (buffer) {
4242
return events;
4343
}
4444

45-
function request(method, headers, path, data, callback) {
45+
function request (method, headers, path, data, callback) {
4646
headers = headers || {};
4747
method = method || 'GET';
4848
path = path || '';
@@ -5184,7 +5184,7 @@ module.exports = (expect) => {
51845184
expect(result.error.message).to.contain('"test"');
51855185

51865186
done();
5187-
5187+
51885188
});
51895189
});
51905190

@@ -5583,6 +5583,122 @@ module.exports = (expect) => {
55835583
});
55845584
});
55855585

5586+
it('Should return a valid /.well-known/ai-plugin.json', done => {
5587+
request('GET', {}, '/.well-known/ai-plugin.json', '', (err, res, result) => {
5588+
5589+
expect(err).to.not.exist;
5590+
expect(res.statusCode).to.equal(200);
5591+
expect(res.headers).to.haveOwnProperty('access-control-allow-origin');
5592+
expect(res.headers).to.haveOwnProperty('access-control-allow-headers');
5593+
expect(res.headers).to.haveOwnProperty('access-control-expose-headers');
5594+
expect(result).to.exist;
5595+
expect(result.schema_version).to.equal('v1');
5596+
expect(result.name_for_human).to.equal('(No name provided)');
5597+
expect(result.name_for_model).to.equal('(No name provided)');
5598+
expect(result.description_for_human).to.equal('(No description provided)');
5599+
expect(result.description_for_model).to.equal('(No description provided)');
5600+
expect(result.api).to.exist;
5601+
expect(result.api.type).to.equal('openapi');
5602+
expect(result.api.url).to.equal('localhost/.well-known/openapi.yaml');
5603+
done();
5604+
5605+
});
5606+
});
5607+
5608+
it('Should return a valid /.well-known/openapi.json', done => {
5609+
request('GET', {}, '/.well-known/openapi.json', '', (err, res, result) => {
5610+
5611+
expect(err).to.not.exist;
5612+
expect(res.statusCode).to.equal(200);
5613+
expect(res.headers).to.haveOwnProperty('access-control-allow-origin');
5614+
expect(res.headers).to.haveOwnProperty('access-control-allow-headers');
5615+
expect(res.headers).to.haveOwnProperty('access-control-expose-headers');
5616+
expect(result).to.exist;
5617+
expect(result.openapi).to.equal('3.1.0');
5618+
expect(result.info).to.exist;
5619+
expect(result.info.version).to.equal('local');
5620+
expect(result.info.title).to.equal('(No name provided)');
5621+
expect(result.info.description).to.equal('(No description provided)');
5622+
expect(result.servers).to.be.an('Array');
5623+
expect(result.servers[0]).to.exist;
5624+
expect(result.servers[0].url).to.equal('localhost');
5625+
expect(result.servers[0].description).to.equal('FunctionScript Gateway');
5626+
expect(result.paths).to.be.an('Object');
5627+
expect(result.paths['/my_function/']).to.exist;
5628+
expect(result.paths['/my_function/'].post).to.exist;
5629+
expect(result.paths['/my_function/'].post.description).to.equal('My function');
5630+
expect(result.paths['/my_function/'].post.operationId).to.equal('service.localhost.my_function');
5631+
expect(result.paths['/my_function/'].post.requestBody).to.exist;
5632+
expect(result.paths['/my_function/'].post.requestBody.content).to.exist;
5633+
expect(result.paths['/my_function/'].post.requestBody.content['application/json']).to.exist;
5634+
expect(result.paths['/my_function/'].post.requestBody.content['application/json']).to.deep.equal({
5635+
"schema": {
5636+
"type": "object",
5637+
"properties": {
5638+
"a": {
5639+
"type": "number",
5640+
"default": 1
5641+
},
5642+
"b": {
5643+
"type": "number",
5644+
"default": 2
5645+
},
5646+
"c": {
5647+
"type": "number",
5648+
"default": 3
5649+
}
5650+
}
5651+
}
5652+
});
5653+
expect(result.paths['/my_function/'].post.responses).to.exist;
5654+
expect(result.paths['/my_function/'].post.responses['200']).to.exist;
5655+
expect(result.paths['/my_function/'].post.responses['200'].content).to.exist;
5656+
expect(result.paths['/my_function/'].post.responses['200'].content['application/json']).to.exist;
5657+
expect(result.paths['/my_function/'].post.responses['200'].content['application/json']).to.deep.equal({
5658+
"schema": {
5659+
"type": "number"
5660+
}
5661+
});
5662+
expect(result.paths['/a_standard_function/']).to.exist;
5663+
expect(result.paths['/reflect/']).to.exist;
5664+
done();
5665+
5666+
});
5667+
});
5668+
5669+
it('Should return a valid /.well-known/openapi.yaml', done => {
5670+
request('GET', {}, '/.well-known/openapi.yaml', '', (err, res, result) => {
5671+
5672+
expect(err).to.not.exist;
5673+
expect(res.statusCode).to.equal(200);
5674+
expect(res.headers).to.haveOwnProperty('access-control-allow-origin');
5675+
expect(res.headers).to.haveOwnProperty('access-control-allow-headers');
5676+
expect(res.headers).to.haveOwnProperty('access-control-expose-headers');
5677+
expect(res.headers['content-type']).to.equal('application/yaml');
5678+
let yaml = result.toString();
5679+
expect(yaml.startsWith('openapi: "3.1.0"')).to.equal(true);
5680+
done();
5681+
5682+
});
5683+
});
5684+
5685+
it('Should return a valid /.well-known/schema.json', done => {
5686+
request('GET', {}, '/.well-known/schema.json', '', (err, res, result) => {
5687+
5688+
expect(err).to.not.exist;
5689+
expect(res.statusCode).to.equal(200);
5690+
expect(res.headers).to.haveOwnProperty('access-control-allow-origin');
5691+
expect(res.headers).to.haveOwnProperty('access-control-allow-headers');
5692+
expect(res.headers).to.haveOwnProperty('access-control-expose-headers');
5693+
expect(res.headers['content-type']).to.equal('application/json');
5694+
expect(result).to.exist;
5695+
expect(result.functions).to.exist;
5696+
expect(result.functions.length).to.be.greaterThan(1);
5697+
done();
5698+
5699+
});
5700+
});
5701+
55865702
after(() => FaaSGateway.close());
55875703

55885704
};

0 commit comments

Comments
 (0)