Skip to content

fix: correct checking for runtime version installed in the project and provide example API endpoint in code snippets #230

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

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export async function netlifyCommonEngineHandler(request: Request, context: any)
// Example API endpoints can be defined here.
// Uncomment and define endpoints as necessary.
// const pathname = new URL(request.url).pathname;
// if (pathname = '/api/hello') {
// if (pathname === '/api/hello') {
// return Response.json({ message: 'Hello from the API' });
// }

Expand All @@ -145,7 +145,7 @@ export async function netlifyAppEngineHandler(request: Request): Promise<Respons
// Example API endpoints can be defined here.
// Uncomment and define endpoints as necessary.
// const pathname = new URL(request.url).pathname;
// if (pathname = '/api/hello') {
// if (pathname === '/api/hello') {
// return Response.json({ message: 'Hello from the API' });
// }

Expand Down
6 changes: 4 additions & 2 deletions src/helpers/getPackageVersion.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { createRequire } = require('node:module')

const { readJSON } = require('fs-extra')

/**
Expand Down Expand Up @@ -29,8 +31,8 @@ module.exports.getAngularVersion = getAngularVersion
const getAngularRuntimeVersion = async function (root) {
let packagePath
try {
// eslint-disable-next-line n/no-missing-require
packagePath = require.resolve('@netlify/angular-runtime/package.json', { paths: [root] })
const siteRequire = createRequire(root)
packagePath = siteRequire.resolve('@netlify/angular-runtime/package.json')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice this was finding the @netlify/angular-runtime installed in <project-dir>/.netlify/plugins/node_modules despite paths being just ['<project-dir'>] (likely due require cache), but this should check user-installed version - this one with createRequire seems to work in practice as intended (it was now tested with monkey patching plugin instance installed in .netlify/plugins)

} catch {
// module not found
return
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/serverModuleHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import { render } from '@netlify/angular-runtime/common-engine'
const commonEngine = new CommonEngine()

export async function netlifyCommonEngineHandler(request: Request, context: any): Promise<Response> {
// Example API endpoints can be defined here.
// Uncomment and define endpoints as necessary.
// const pathname = new URL(request.url).pathname;
// if (pathname === '/api/hello') {
// return Response.json({ message: 'Hello from the API' });
// }

return await render(commonEngine)
}
`
Expand All @@ -29,6 +36,13 @@ const angularAppEngine = new AngularAppEngine()
export async function netlifyAppEngineHandler(request: Request): Promise<Response> {
const context = getContext()

// Example API endpoints can be defined here.
// Uncomment and define endpoints as necessary.
// const pathname = new URL(request.url).pathname;
// if (pathname === '/api/hello') {
// return Response.json({ message: 'Hello from the API' });
// }

const result = await angularAppEngine.handle(request, context)
return result || new Response('Not found', { status: 404 })
}
Expand Down
Loading