Skip to content

Commit

Permalink
feat: internal request api now integrates with the CacheClock module
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Nov 19, 2022
1 parent 03fc4a9 commit 57de0d8
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions lib/core/api/request-api.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { CacheClock } from "cache-clock";

import type { UrbexContext } from "../../environment";
import type { InternalConfiguration } from "../../exportable-types";
import type { InternalConfiguration, UrbexResponse } from "../../exportable-types";
import type { DispatchedResponse, UrbexRequestApi } from "../../types";

import { NodeRequest } from "./http";
import { BrowserRequest } from "./xhr";
import { startRequest } from "./conclude";
import { environment } from "../../environment";
import { UrbexError } from "../error";
import { UrbexHeaders } from "../headers";
import { createPromise, replaceObjectProperty, isEmpty, isObject, merge } from "../../utils";
import { convertURIComponentToString } from "../url";
import { clone } from "../../utils";

// here all of the interceptors are checked
// cache clocks are checked here
Expand Down Expand Up @@ -51,8 +52,53 @@ export class RequestApi {
}

protected async dispatchRequest(config: InternalConfiguration): DispatchedResponse {
console.log(config);
try {
const configuration = clone(config);
const concludeRequest = startRequest(configuration);

return this.$api.send(config);
// for some odd reason, result.cache had this weird mutation
// issue even when CLONING the result, so I had to do this
// to get it to work properly

const cache: UrbexResponse["cache"] = {
key: null,
hit: false,
pulled: false,
stored: false
};

if (configuration.cache && configuration.cache.enabled) {
const cacheKey = this.$cache.getCacheKey(configuration.url.href);
const entity = this.$cache.get(cacheKey, true);

cache.hit = true;

if (entity) {
const result = await concludeRequest({ data: entity.v });

cache.key = cacheKey;
cache.pulled = true;
result.cache = cache;

return Promise.resolve(result);
}
}

const response = await this.$api.send(configuration);
const result = await concludeRequest(response);

if (configuration.cache && configuration.cache.enabled) {
this.$cache.set(configuration.url.href, response.data);

cache.key = this.$cache.getCacheKey(configuration.url.href);
cache.stored = true;
}

result.cache = cache;

return Promise.resolve(result);
} catch (error: any) {
return Promise.reject(new UrbexError(error));
}
}
}

0 comments on commit 57de0d8

Please sign in to comment.