Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
81fedf45
Commit
81fedf45
authored
Dec 30, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix csrf request
parent
e55e987f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
10 deletions
+34
-10
buildUrlNode.ts
lib/api/buildUrlNode.ts
+24
-0
nodeFetch.ts
lib/api/nodeFetch.ts
+2
-7
csrf.ts
pages/api/csrf.ts
+2
-2
proxy.ts
pages/api/proxy.ts
+6
-1
No files found.
lib/api/buildUrlNode.ts
0 → 100644
View file @
81fedf45
import
{
compile
}
from
'
path-to-regexp
'
;
import
appConfig
from
'
configs/app/config
'
;
import
{
RESOURCES
}
from
'
./resources
'
;
import
type
{
ApiResource
,
ResourceName
}
from
'
./resources
'
;
export
default
function
buildUrlNode
(
_resource
:
ApiResource
|
ResourceName
,
pathParams
?:
Record
<
string
,
string
|
undefined
>
,
queryParams
?:
Record
<
string
,
string
|
number
|
undefined
>
,
)
{
const
resource
:
ApiResource
=
typeof
_resource
===
'
string
'
?
RESOURCES
[
_resource
]
:
_resource
;
const
baseUrl
=
resource
.
endpoint
||
appConfig
.
api
.
endpoint
;
const
basePath
=
resource
.
basePath
!==
undefined
?
resource
.
basePath
:
appConfig
.
api
.
basePath
;
const
path
=
basePath
+
resource
.
path
;
const
url
=
new
URL
(
compile
(
path
)(
pathParams
),
baseUrl
);
queryParams
&&
Object
.
entries
(
queryParams
).
forEach
(([
key
,
value
])
=>
{
value
&&
url
.
searchParams
.
append
(
key
,
String
(
value
));
});
return
url
.
toString
();
}
lib/api/nodeFetch.ts
View file @
81fedf45
...
@@ -2,25 +2,20 @@ import type { NextApiRequest } from 'next';
...
@@ -2,25 +2,20 @@ import type { NextApiRequest } from 'next';
import
type
{
RequestInit
,
Response
}
from
'
node-fetch
'
;
import
type
{
RequestInit
,
Response
}
from
'
node-fetch
'
;
import
nodeFetch
from
'
node-fetch
'
;
import
nodeFetch
from
'
node-fetch
'
;
import
appConfig
from
'
configs/app/config
'
;
import
{
httpLogger
}
from
'
lib/api/logger
'
;
import
{
httpLogger
}
from
'
lib/api/logger
'
;
import
*
as
cookies
from
'
lib/cookies
'
;
import
*
as
cookies
from
'
lib/cookies
'
;
export
default
function
fetchFactory
(
export
default
function
fetchFactory
(
_req
:
NextApiRequest
,
_req
:
NextApiRequest
,
apiEndpoint
:
string
=
appConfig
.
api
.
endpoint
,
)
{
)
{
// first arg can be only a string
// first arg can be only a string
// FIXME migrate to RequestInfo later if needed
// FIXME migrate to RequestInfo later if needed
return
function
fetch
(
path
:
string
,
init
?:
RequestInit
):
Promise
<
Response
>
{
return
function
fetch
(
url
:
string
,
init
?:
RequestInit
):
Promise
<
Response
>
{
const
csrfToken
=
_req
.
headers
[
'
x-csrf-token
'
]?.
toString
();
const
headers
=
{
const
headers
=
{
accept
:
'
application/json
'
,
accept
:
'
application/json
'
,
'
content-type
'
:
'
application/json
'
,
'
content-type
'
:
'
application/json
'
,
cookie
:
`
${
cookies
.
NAMES
.
API_TOKEN
}
=
${
_req
.
cookies
[
cookies
.
NAMES
.
API_TOKEN
]
}
`
,
cookie
:
`
${
cookies
.
NAMES
.
API_TOKEN
}
=
${
_req
.
cookies
[
cookies
.
NAMES
.
API_TOKEN
]
}
`
,
...(
csrfToken
?
{
'
x-csrf-token
'
:
csrfToken
}
:
{}),
};
};
const
url
=
new
URL
(
path
,
apiEndpoint
);
httpLogger
.
logger
.
info
({
httpLogger
.
logger
.
info
({
message
:
'
Trying to call API
'
,
message
:
'
Trying to call API
'
,
...
@@ -28,7 +23,7 @@ export default function fetchFactory(
...
@@ -28,7 +23,7 @@ export default function fetchFactory(
req
:
_req
,
req
:
_req
,
});
});
return
nodeFetch
(
url
.
toString
()
,
{
return
nodeFetch
(
url
,
{
headers
,
headers
,
...
init
,
...
init
,
});
});
...
...
pages/api/csrf.ts
View file @
81fedf45
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'
next
'
;
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'
next
'
;
import
buildUrl
from
'
lib/api/buildUrl
'
;
import
buildUrl
Node
from
'
lib/api/buildUrlNode
'
;
import
{
httpLogger
}
from
'
lib/api/logger
'
;
import
{
httpLogger
}
from
'
lib/api/logger
'
;
import
fetchFactory
from
'
lib/api/nodeFetch
'
;
import
fetchFactory
from
'
lib/api/nodeFetch
'
;
export
default
async
function
csrfHandler
(
_req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
export
default
async
function
csrfHandler
(
_req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
httpLogger
(
_req
,
res
);
httpLogger
(
_req
,
res
);
const
url
=
buildUrl
(
'
csrf
'
);
const
url
=
buildUrl
Node
(
'
csrf
'
);
const
response
=
await
fetchFactory
(
_req
)(
url
);
const
response
=
await
fetchFactory
(
_req
)(
url
);
if
(
response
.
status
===
200
)
{
if
(
response
.
status
===
200
)
{
...
...
pages/api/proxy.ts
View file @
81fedf45
...
@@ -2,6 +2,7 @@ import _pick from 'lodash/pick';
...
@@ -2,6 +2,7 @@ import _pick from 'lodash/pick';
import
_pickBy
from
'
lodash/pickBy
'
;
import
_pickBy
from
'
lodash/pickBy
'
;
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'
next
'
;
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'
next
'
;
import
appConfig
from
'
configs/app/config
'
;
import
fetchFactory
from
'
lib/api/nodeFetch
'
;
import
fetchFactory
from
'
lib/api/nodeFetch
'
;
const
handler
=
async
(
_req
:
NextApiRequest
,
res
:
NextApiResponse
)
=>
{
const
handler
=
async
(
_req
:
NextApiRequest
,
res
:
NextApiResponse
)
=>
{
...
@@ -10,8 +11,12 @@ const handler = async(_req: NextApiRequest, res: NextApiResponse) => {
...
@@ -10,8 +11,12 @@ const handler = async(_req: NextApiRequest, res: NextApiResponse) => {
return
;
return
;
}
}
const
response
=
await
fetchFactory
(
_req
,
_req
.
headers
[
'
x-endpoint
'
]?.
toString
())
(
const
url
=
new
URL
(
_req
.
url
.
replace
(
/^
\/
node-api
\/
proxy/
,
''
),
_req
.
url
.
replace
(
/^
\/
node-api
\/
proxy/
,
''
),
_req
.
headers
[
'
x-endpoint
'
]?.
toString
()
||
appConfig
.
api
.
endpoint
,
);
const
response
=
await
fetchFactory
(
_req
)(
url
.
toString
(),
_pickBy
(
_pick
(
_req
,
[
'
body
'
,
'
method
'
]),
Boolean
),
_pickBy
(
_pick
(
_req
,
[
'
body
'
,
'
method
'
]),
Boolean
),
);
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment