Replies: 3 comments 5 replies
-
|
The |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
@unnoq Do you know why the bot Dosu didn't replied? |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Here is an example with code with two appraoch:
1 is correct for Typescript, instead 2 is incorrect for TS. import { os } from "@orpc/server";
const builderWithCache = os
.$meta<{ cache: boolean; }>({
cache: false
})
.use(async (stuff, input) => {
// if cache not enable -> skip
if (!stuff.procedure["~orpc"].meta.cache) return stuff.next();
// get cached data
const cached = getCachedByProcedureData({
procedureKey: stuff.procedure["~orpc"].route.operationId ?? stuff.procedure["~orpc"].route.path,
inputNotSerialized: input
});
// if cache -> return from cache
if (cached.cacheData) return cached.cacheData;
// if not cached -> create cache + return
const result = await stuff.next();
saveToCache(cached.cacheKey, result);
return result;
});
const middlewareCache = os
.$meta<{ cache: boolean; }>({
cache: false
})
.middleware(async (stuff, input) => {
// if cache not enable -> skip
if (!stuff.procedure["~orpc"].meta.cache) return stuff.next();
// get cached data
const cached = getCachedByProcedureData({
procedureKey: stuff.procedure["~orpc"].route.operationId ?? stuff.procedure["~orpc"].route.path,
inputNotSerialized: input
});
// if cache -> return from cache
if (cached.cacheData) return cached.cacheData;
// if not cached -> create cache + return
const result = await stuff.next();
saveToCache(cached.cacheKey, result);
return result;
});
// with builder
const procWithBuilder = builderWithCache
.meta({
cache: true,
})
.handler(async () => {
return {
data: {
hello: 'world'
}
};
});
// with middleware
const procWithMiddleware = os
.use(middlewareCache)
/*
❌🔥 TS ERROR ☝️
Argument of type 'DecoratedMiddleware<Record<never, never>, Record<never, never>, unknown, any, any, { cache: boolean; } & Record<never, never>>' is not assignable to parameter of type 'Middleware<Record<never, never>, Record<never, never>, unknown, unknown, ORPCErrorConstructorMap<Record<never, never>>, Record<never, never>>'.
Types of parameters 'options' and 'options' are incompatible.
Type 'MiddlewareOptions<Record<never, never>, unknown, ORPCErrorConstructorMap<Record<never, never>>, Record<never, never>>' is not assignable to type 'MiddlewareOptions<Record<never, never>, any, any, { cache: boolean; } & Record<never, never>>'.
Type 'Record<never, never>' is not assignable to type '{ cache: boolean; } & Record<never, never>'.
Property 'cache' is missing in type 'Record<never, never>' but required in type '{ cache: boolean; }'
*/
.meta({
cache: true
})
.handler(async () => {
return {
data: {
hello: 'world'
}
};
});
// internal - cache
function getCachedByProcedureData(data: {
procedureKey?: string,
inputNotSerialized?: any,
}) {
// enure procedere key
if (!data.procedureKey) {
throw new Error("Cannot create cache key: Procedure key not found");
}
// ensure input are serializable
let inputStringified: string | undefined;
try {
inputStringified = JSON.stringify(data.inputNotSerialized ?? {});
} catch (error) {
throw new Error("Cannot create cache key: Failed to stringify input");
}
// create cache key
const cacheKey = `procedure:${data.procedureKey}__input:${inputStringified}`;
// get from cache
// TODO: do it
return {
cacheKey,
cacheData: null,
}
}
function saveToCache(cacheKey: string, cacheData: any) {
// TODO: do it
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Don't want to waste time of anyone, so @dosu please partecipate the conversation and include code examples with clear comments of "what is happening behind the scene".
In orpc there is the concept of
metadata, with$metaandmetaAPIs.Questions:
$metais for definition of whichmetais required to be set? or what else?$metacan be added to both middleware and procedure? What it does on both?metacan be added to both middleware and procedure? What it does on both?handlerand outside of it?Beta Was this translation helpful? Give feedback.
All reactions