Skip to content

Commit 3423c3c

Browse files
authored
Merge pull request #42 from imranhsayed/feature/add-endpoint
Add api endpoint '/api/cors' with cors example
2 parents 5e0e195 + a62c7d3 commit 3423c3c

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

backend/wordpress/.htaccess

+8
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
33
# dynamically generated, and should only be modified via WordPress filters.
44
# Any changes to the directives between these markers will be overwritten.
5+
<IfModule mod_rewrite.c>
6+
RewriteEngine On
7+
RewriteBase /
8+
RewriteRule ^index\.php$ - [L]
9+
RewriteCond %{REQUEST_FILENAME} !-f
10+
RewriteCond %{REQUEST_FILENAME} !-d
11+
RewriteRule . /index.php [L]
12+
</IfModule>
513

614
# END WordPress

frontend/lib/init-middleware.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Helper method to wait for a middleware to execute before continuing
2+
// And to throw an error when an error happens in a middleware
3+
export default function initMiddleware(middleware) {
4+
return (req, res) =>
5+
new Promise((resolve, reject) => {
6+
middleware(req, res, (result) => {
7+
if (result instanceof Error) {
8+
return reject(result)
9+
}
10+
return resolve(result)
11+
})
12+
})
13+
}

frontend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"homepage": "https://github.com/imranhsayed/nextjs-headless-wordpress#readme",
2323
"dependencies": {
2424
"@apollo/client": "^3.1.4",
25+
"cors": "^2.8.5",
2526
"disqus-react": "^1.0.10",
2627
"dompurify": "^2.0.15",
2728
"graphql": "^15.3.0",

frontend/pages/api/cors.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Cors from 'cors'
2+
import initMiddleware from '../../lib/init-middleware'
3+
4+
// Initialize the cors middleware
5+
const cors = initMiddleware(
6+
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
7+
Cors({
8+
// Only allow requests with GET, POST and OPTIONS
9+
methods: ['GET', 'POST', 'OPTIONS'],
10+
})
11+
)
12+
13+
export default async function handler(req, res) {
14+
// Run cors
15+
await cors(req, res)
16+
17+
// Rest of the API logic
18+
res.json({ message: 'Hello Everyone!' })
19+
}

frontend/yarn.lock

+14-1
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,14 @@ [email protected], core-util-is@~1.0.0:
23672367
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
23682368
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=
23692369

2370+
cors@^2.8.5:
2371+
version "2.8.5"
2372+
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
2373+
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
2374+
dependencies:
2375+
object-assign "^4"
2376+
vary "^1"
2377+
23702378
create-ecdh@^4.0.0:
23712379
version "4.0.4"
23722380
resolved "https://registry.yarnpkg.com/create-ecdh/-/create-ecdh-4.0.4.tgz#d6e7f4bffa66736085a0762fd3a632684dabcc4e"
@@ -4758,7 +4766,7 @@ oauth-sign@~0.9.0:
47584766
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
47594767
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==
47604768

4761-
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
4769+
object-assign@^4, object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
47624770
version "4.1.1"
47634771
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
47644772
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
@@ -6902,6 +6910,11 @@ uuid@^3.3.2:
69026910
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
69036911
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
69046912

6913+
vary@^1:
6914+
version "1.1.2"
6915+
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
6916+
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
6917+
69056918
69066919
version "1.10.0"
69076920
resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400"

0 commit comments

Comments
 (0)