-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow a custom storage for the api caches
- Loading branch information
Showing
16 changed files
with
119 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
export { | ||
createCachedCoaApi, | ||
createCachedUserApi, | ||
getCommonCacheOptions, | ||
} from "./src/api"; | ||
import { bookcase } from "./src/stores/bookcase"; | ||
import { coa } from "./src/stores/coa"; | ||
import * as apiUtil from "./src/util/api"; | ||
export const stores = { | ||
coa, | ||
bookcase, | ||
}; | ||
|
||
export const util = apiUtil; | ||
|
||
export { default as i18n } from "./src/i18n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import axios from "axios"; | ||
import { | ||
CacheOptions, | ||
CacheRequestConfig, | ||
setupCache, | ||
} from "axios-cache-interceptor"; | ||
import dayjs from "dayjs"; | ||
|
||
import { addUrlParamsRequestInterceptor } from "~axios-helper"; | ||
|
||
const now = dayjs(); | ||
const inAnHour = dayjs().add(1, "hour"); | ||
|
||
let coaCacheExpiration = dayjs(); | ||
if (now.get("hour") >= 4) { | ||
coaCacheExpiration = coaCacheExpiration.add(1, "day"); | ||
} | ||
coaCacheExpiration = coaCacheExpiration | ||
.set("hour", 4) | ||
.set("minute", 0) | ||
.set("second", 0) | ||
.set("millisecond", 0); | ||
|
||
export const getCommonCacheOptions = ( | ||
storage: CacheOptions["storage"], | ||
): CacheOptions => ({ | ||
etag: false, | ||
modifiedSince: false, | ||
interpretHeader: false, | ||
generateKey: (options: CacheRequestConfig) => | ||
`${options.url}${ | ||
options.params ? `?${new URLSearchParams(options.params).toString()}` : "" | ||
}`, | ||
storage: storage, | ||
}); | ||
|
||
export const createCachedCoaApi = ( | ||
storage: CacheOptions["storage"], | ||
baseURL: string, | ||
) => | ||
addUrlParamsRequestInterceptor( | ||
setupCache( | ||
axios.create({ | ||
baseURL, | ||
}), | ||
{ | ||
...getCommonCacheOptions(storage), | ||
methods: ["get", "post"], | ||
ttl: coaCacheExpiration.diff(now), | ||
}, | ||
), | ||
); | ||
|
||
export const createCachedUserApi = ( | ||
storage: CacheOptions["storage"], | ||
baseURL: string, | ||
) => | ||
addUrlParamsRequestInterceptor( | ||
setupCache( | ||
axios.create({ | ||
baseURL, | ||
}), | ||
{ | ||
...getCommonCacheOptions(storage), | ||
ttl: inAnHour.diff(now), | ||
}, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { BookstoreCommentEvent } from "./events/BookstoreCommentEvent"; | ||
import { CollectionSubscriptionAdditionEvent } from "./events/CollectionSubscriptionAdditionEvent"; | ||
import { CollectionUpdateEvent } from "./events/CollectionUpdateEvent"; | ||
import { EdgeCreationEvent } from "./events/EdgeCreationEvent"; | ||
import { MedalEvent } from "./events/MedalEvent"; | ||
import { SignupEvent } from "./events/SignupEvent"; | ||
|
||
export type Event = | ||
| SignupEvent | ||
| CollectionUpdateEvent | ||
| CollectionSubscriptionAdditionEvent | ||
| BookstoreCommentEvent | ||
| EdgeCreationEvent | ||
| MedalEvent; |
This file was deleted.
Oops, something went wrong.