Skip to content

Commit

Permalink
feat: install ky and create base instance
Browse files Browse the repository at this point in the history
  • Loading branch information
timepresent95 committed Aug 12, 2024
1 parent 4ca44e8 commit 9eba9fe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@react-navigation/native": "^6.1.18",
"@react-navigation/native-stack": "^6.11.0",
"@tanstack/react-query": "^5.51.18",
"ky": "^1.5.0",
"nativewind": "^2.0.11",
"react": "18.2.0",
"react-native": "0.74.3",
Expand Down
28 changes: 28 additions & 0 deletions src/api/ky.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import ky from 'ky';

import {API_BASE_URL} from '@/constants/api';

const requestHeaders: Record<string, string> = {};

const kyInstance = ky.create({
prefixUrl: API_BASE_URL,
hooks: {
beforeRequest: [
request => {
Object.keys(requestHeaders).forEach(key => {
request.headers.set(key, requestHeaders[key]);
});
},
],
},
});

function setHeader(key: string, value: string) {
requestHeaders[key] = value;
}

function removeHeader(key: string) {
delete requestHeaders[key];
}

export {kyInstance, setHeader, removeHeader};

0 comments on commit 9eba9fe

Please sign in to comment.