diff --git a/access-functions.php b/access-functions.php index 10d541e..98f6c2b 100644 --- a/access-functions.php +++ b/access-functions.php @@ -72,8 +72,8 @@ function snapwp_helper_get_env_variables() { $upload_path = '/' . ltrim( str_replace( ABSPATH, '', $upload_dir['basedir'] ), '/' ); return [ - 'NODE_TLS_REJECT_UNAUTHORIZED' => '', - 'NEXT_PUBLIC_URL' => '', + 'NODE_TLS_REJECT_UNAUTHORIZED' => '0', + 'NEXT_PUBLIC_URL' => 'http://localhost:3000', 'NEXT_PUBLIC_WORDPRESS_URL' => untrailingslashit( get_home_url() ), 'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => graphql_get_endpoint(), 'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => $upload_path, diff --git a/docs/rest-api.md b/docs/rest-api.md index a978476..9a2ce70 100644 --- a/docs/rest-api.md +++ b/docs/rest-api.md @@ -23,7 +23,7 @@ curl -X GET \ This endpoint does not require any parameters to be passed in the request body. The .env file content is generated based on WordPress settings. Unchanged variables will be commented out. - - `NODE_TLS_REJECT_UNAUTHORIZED`: Enable if connecting to a self-signed cert. (Default: `0`) + - `NODE_TLS_REJECT_UNAUTHORIZED`: Only enable if connecting to a self-signed cert. (Default: `0`) - `NEXT_PUBLIC_URL` (Required): The headless frontend domain URL. (Default: `http://localhost:3000`) - `NEXT_PUBLIC_WORDPRESS_URL` (Required): The WordPress "frontend" domain URL. - `NEXT_PUBLIC_GRAPHQL_ENDPOINT`: The WordPress GraphQL endpoint. (Default: `graphql`) diff --git a/src/Modules/EnvGenerator/Generator.php b/src/Modules/EnvGenerator/Generator.php index 9cec7f9..44ac4e0 100644 --- a/src/Modules/EnvGenerator/Generator.php +++ b/src/Modules/EnvGenerator/Generator.php @@ -95,7 +95,8 @@ protected function prepare_variable( string $name, ?string $value ): ?string { $required = ! empty( $variable['required'] ); // Check if a required variable has a value. - if ( $required && empty( $value ) ) { + // @todo: handle NODE_TLS_REJECT_UNAUTHORIZED by checking the NEXT_PUBLIC_URL. + if ( $required && empty( $value ) && '0' !== $value ) { throw new \InvalidArgumentException( 'Required variables must have a value.' ); } diff --git a/src/Modules/EnvGenerator/VariableRegistry.php b/src/Modules/EnvGenerator/VariableRegistry.php index 3a95714..ce47f11 100644 --- a/src/Modules/EnvGenerator/VariableRegistry.php +++ b/src/Modules/EnvGenerator/VariableRegistry.php @@ -20,14 +20,14 @@ class VariableRegistry { */ private const VARIABLES = [ 'NODE_TLS_REJECT_UNAUTHORIZED' => [ - 'description' => 'Enable if connecting to a self-signed cert', + 'description' => 'Only enable if connecting to a self-signed cert', 'default' => '0', - 'required' => false, + 'required' => true, ], 'NEXT_PUBLIC_URL' => [ - 'description' => 'The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.', + 'description' => 'The headless frontend domain URL. Make sure the value matches the URL used by your frontend app.', 'default' => 'http://localhost:3000', - 'required' => false, + 'required' => true, ], 'NEXT_PUBLIC_WORDPRESS_URL' => [ 'description' => 'The WordPress "frontend" domain URL', diff --git a/tests/Integration/GeneratorTest.php b/tests/Integration/GeneratorTest.php index 2f1cf5e..0e10fcc 100644 --- a/tests/Integration/GeneratorTest.php +++ b/tests/Integration/GeneratorTest.php @@ -58,11 +58,11 @@ public function testGenerateEnvContent(): void { $content = $generator->generate(); $expectedContent = ' -# Enable if connecting to a self-signed cert +# Only enable if connecting to a self-signed cert NODE_TLS_REJECT_UNAUTHORIZED=5 -# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app. -# NEXT_PUBLIC_URL=http://localhost:3000 +# The headless frontend domain URL. Make sure the value matches the URL used by your frontend app. +NEXT_PUBLIC_URL=http://localhost:3000 # The WordPress "frontend" domain URL NEXT_PUBLIC_WORDPRESS_URL=https://headless-demo.local @@ -111,8 +111,8 @@ public function testDefaultValuesForEnvContent(): void { // CASE : For NODE_TLS_REJECT_UNAUTHORIZED with no default value, Generator class should comment out the variable in .ENV content. $values = [ - 'NODE_TLS_REJECT_UNAUTHORIZED' => '', - 'NEXT_PUBLIC_URL' => '', + 'NODE_TLS_REJECT_UNAUTHORIZED' => '0', + 'NEXT_PUBLIC_URL' => 'http://localhost:3000', 'NEXT_PUBLIC_WORDPRESS_URL' => 'https://headless-demo.local', 'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => '/test_endpoint', 'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => '', @@ -126,11 +126,11 @@ public function testDefaultValuesForEnvContent(): void { // Define expected content. $expectedContent = ' -# Enable if connecting to a self-signed cert -# NODE_TLS_REJECT_UNAUTHORIZED=0 +# Only enable if connecting to a self-signed cert +NODE_TLS_REJECT_UNAUTHORIZED=0 -# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app. -# NEXT_PUBLIC_URL=http://localhost:3000 +# The headless frontend domain URL. Make sure the value matches the URL used by your frontend app. +NEXT_PUBLIC_URL=http://localhost:3000 # The WordPress "frontend" domain URL NEXT_PUBLIC_WORDPRESS_URL=https://headless-demo.local diff --git a/tests/Integration/RestControllerTest.php b/tests/Integration/RestControllerTest.php index 392b52f..535e5fb 100644 --- a/tests/Integration/RestControllerTest.php +++ b/tests/Integration/RestControllerTest.php @@ -88,7 +88,7 @@ public function testGenerateEnvEndpoint(): void { $this->assertNotEmpty( $actual_data['content'] ); $search = '\n'; $replace = ''; - $expected = "\n# Enable if connecting to a self-signed cert\n# NODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.\n# NEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\nNEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=/" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/" . rest_get_url_prefix() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token(); + $expected = "\n# Only enable if connecting to a self-signed cert\nNODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL. Make sure the value matches the URL used by your frontend app.\nNEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\nNEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=/" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/" . rest_get_url_prefix() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token(); $this->assertEquals( $expected, str_replace( $search, $replace, $actual_data['content'] ) );