Note: it is HIGHLY recommended that you use TypeScript. The typing system using in TypeScript will let you produce a much more reliable and better documented API
This package can be added to an existing Node project via the command
npm install @automatique/autoapi-js
AutoAPI-JS currently exports two major functions, buildJSExpress
and buildTSExpress
. The purpose
of this package is to automatically build REST APIs from simple JavaScript/TypeScript functions. The functions
that are turned into APIs are those that are in the default export
(typescript) or module.exports =
(javascript)
of the main file specified to either buildJSExpress
or buildTSExpress
.
index.ts
:
function hello(): string {
return "Hello world";
}
export default {
hello: hello,
};
Running the below buildTSExpress function with the above snippet in index.ts
will generate an API,
with a single path, /hello
which executes and returns the result of the function hello()
// Build an API
buildTSExpress(
// Project root
"/some/path/to/a/typescript/project",
// Main file
"index.ts"
// Optional filename to import from (eg if moving the main file to another name)
"app.ts"
);
The above returns an object with three attributes, "index", the text of an index.ts script, "packageJSON", a package.json file, and "tsConfig", a valid typescript config.
index.js
function hello() {
return "Hello world";
}
module.exports = {
hello,
};
// Build an API
buildJSExpress(
// Project root
"/some/path/to/a/typescript/project",
// Main file
"index.js"
);
There are some changes in the source file that must be made to correctly work
- URL Paths are case-insensitive, unlike JavaScript/TypeScript identifiers. If you attempt to export 2 functions with the same name, differing by casing (eg foo() and FOO()), undefined behaviour will occur
autoapi-js
is an open-source project by Automatique. If you encounter bugs, find problems or just want to contribute features,
feel free to either open an issue or fork and make a pull request