Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose API #6

Closed
8 tasks done
8eecf0d2 opened this issue Oct 15, 2018 · 7 comments
Closed
8 tasks done

Expose API #6

8eecf0d2 opened this issue Oct 15, 2018 · 7 comments
Assignees
Labels
enhancement New feature or request implemented Implemented in master but the issue is still relevant

Comments

@8eecf0d2
Copy link
Owner

8eecf0d2 commented Oct 15, 2018

Would be handy to expose an API so test runners can start & stop the server, optionally with the build system.

Usage

import { parseNetlifyConfig, parseWebpackConfig, Server, Webpack } from "netlify-local";

const netlifyConfig = parseNetlifyConfig("netlify.toml");
const port = 3000;
const server = new Server({
  netlifyConfig: netlifyConfig,
  routes: {
    static: true,
    lambda: true,
  },
  certificates: {
    key: <private key as a string>,
    cert: <certificate as a string>,
  },
  port: 9000,
});
await server.listen();
await server.close();

const webpackConfig = parseWebpackConfig("webpack.config.js");
const webpack = new Webpack(webpackConfig);
await webpack.build();
webpack.watch();

Methods

Config

  • parseNetlifyConfig()
  • parseWebpackConfig()

Server

  • constructor
  • Server.listen()
  • Server.close()

Webpack

  • constructor
  • Webpack.build()
  • Webpack.watch()
@8eecf0d2
Copy link
Owner Author

Keeping this open until thoroughly tested.

@8eecf0d2 8eecf0d2 added the implemented Implemented in master but the issue is still relevant label Oct 15, 2018
@levino
Copy link

levino commented Oct 16, 2018

I tried it. No success.

import { parseNetlifyConfig, Server } from 'netlify-local'
import request from 'superagent'
import test from 'tape'
const startServer: () => Promise<Server> = async () => {
  const server = new Server(parseNetlifyConfig('netlify.toml'), 3005)
  await server.listen()
  return server
}

test('Integration test', (t) => {
  t.test('Start a lambda function', async (assert) => {
    assert.plan(1)
    const server = await startServer()
    const response = await request.get('http://localhost:3005/myFunction')
    assert.ok(response)
    await server.close()
  })
})

and I get

TAP version 13
# Integration test
# Start a lambda function
(node:7901) UnhandledPromiseRejectionWarning: TypeError: Path must be a string. Received undefined

I think my netlify.toml is misconfigured. What do you expect in the file?

@8eecf0d2
Copy link
Owner Author

Hmm, currently writing some Server tests so I'll see if I can reproduce, all I can suggest for now is to try with 0.4.0.

8eecf0d2 added a commit that referenced this issue Oct 16, 2018
@8eecf0d2
Copy link
Owner Author

Unable to reproduce unfortunately, I'm guessing you've got some more exotic toml usage which netlify-local is not able to handle gracefully - although a TypeError seems odd..

If you're able to provide your netlify.toml or something with similar structure I'd be happy to test further.

@levino
Copy link

levino commented Oct 16, 2018

The error was because I do not have a publish entry in my netlify.toml. I do things differently now: I write express handlers instead of lambda function handlers. I then use serverless-http to bundle one app per express handler as a lambda function handler. I then test my express apps and hope that the bundling via serverless-http does not make any issues.

@8eecf0d2
Copy link
Owner Author

Updated OP with correct API usage as of 1.0.0.

@8eecf0d2
Copy link
Owner Author

Closing this as it appears to be working as expected in tests and real world usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request implemented Implemented in master but the issue is still relevant
Projects
None yet
Development

No branches or pull requests

2 participants