@panter/cloud-tasks 1.1.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var p=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var i=(o,t)=>p(o,"name",{value:t,configurable:!0});var x=(o,t)=>{for(var e in t)p(o,e,{get:t[e],enumerable:!0})},I=(o,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of E(t))!v.call(o,s)&&s!==e&&p(o,s,{get:()=>t[s],enumerable:!(r=q(t,s))||r.enumerable});return o};var S=o=>I(p({},"__esModule",{value:!0}),o);var L={};x(L,{createTasksClient:()=>O});module.exports=S(L);var f=require("@google-cloud/tasks"),C=require("@grpc/grpc-js"),R=require("google-auth-library"),g=require("google-gax"),a=require("ts-results-es");var h=class{constructor(t,e,r,s){this.client=t;this.location=e;this.queueName=r;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new R.GoogleAuth().getCredentials().then(n=>n.client_email??null)}static{i(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting tasks queue");let n="create";try{await this.client.getQueue({name:r}),n="update"}catch(u){if(k(u)&&u.code===g.Status.NOT_FOUND)n="create";else throw u}return n==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:r,...t}})),a.Ok.EMPTY}catch(e){return(0,a.Err)(l(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return(0,a.Ok)(r)}catch(t){return(0,a.Err)(l(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),n=this.client.taskPath(r,this.location,this.queueName,N(t)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:n,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,a.Ok)(n)}catch(r){return(0,a.Err)(l(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,a.Ok)(e)}catch(e){return k(e)&&e.code===g.Status.NOT_FOUND?(0,a.Ok)(null):(0,a.Err)(l(e))}}},m=class extends h{static{i(this,"RegularTasksClient")}constructor(t,e,r){super(new f.CloudTasksClient({}),t,e,r)}},d=class extends h{static{i(this,"EmulatorTasksClient")}constructor(t,e,r,s){super(new f.CloudTasksClient({sslCreds:C.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:r,...t}}),a.Ok.EMPTY}catch(n){return k(n)&&n.code!==g.Status.ALREADY_EXISTS?(0,a.Err)(l(n)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),a.Ok.EMPTY)}}catch(e){return(0,a.Err)(l(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function N(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${new Date().getTime()}-${e}`}i(N,"createUniqueName");function k(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}i(k,"isRpcError");function l(o){return k(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}i(l,"toTasksError");function O({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:s,logger:n={error:console.error,info:console.info,warn:console.warn}}){n.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let u=t?new d(e,o,n,t):new m(e,o,n);return new Proxy({},{get:i((j,y)=>y==="_manage"?{find:i(c=>u.getTask(c).then(T=>T.unwrap()),"find"),list:i(()=>u.listTasks().then(c=>c.unwrap()),"list")}:{schedule:i(async(c,T)=>{let w=y.toString();await u.upsertQueue(s);let P=new URL(`/trpc/${w}`,r).toString();return n.info({url:P,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await u.createTask(w,{...T,httpRequest:{httpMethod:"POST",url:P,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}i(O,"createTasksClient");
1
+ "use strict";var p=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var i=(o,t)=>p(o,"name",{value:t,configurable:!0});var I=(o,t)=>{for(var e in t)p(o,e,{get:t[e],enumerable:!0})},x=(o,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of E(t))!v.call(o,s)&&s!==e&&p(o,s,{get:()=>t[s],enumerable:!(r=q(t,s))||r.enumerable});return o};var S=o=>x(p({},"__esModule",{value:!0}),o);var L={};I(L,{createTasksClient:()=>O});module.exports=S(L);var f=require("@google-cloud/tasks"),R=require("@grpc/grpc-js"),C=require("google-auth-library"),g=require("google-gax"),a=require("ts-results-es");var h=class{constructor(t,e,r,s){this.client=t;this.location=e;this.queueName=r;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new C.GoogleAuth().getCredentials().then(n=>n.client_email??null)}static{i(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting tasks queue");let n="create";try{await this.client.getQueue({name:r}),n="update"}catch(u){if(k(u)&&u.code===g.Status.NOT_FOUND)n="create";else throw u}return n==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:r,...t}})),a.Ok.EMPTY}catch(e){return(0,a.Err)(l(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return(0,a.Ok)(r)}catch(t){return(0,a.Err)(l(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),n=this.client.taskPath(r,this.location,this.queueName,N(t)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:n,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,a.Ok)(n)}catch(r){return(0,a.Err)(l(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,a.Ok)(e)}catch(e){return k(e)&&e.code===g.Status.NOT_FOUND?(0,a.Ok)(null):(0,a.Err)(l(e))}}},m=class extends h{static{i(this,"RegularTasksClient")}constructor(t,e,r){super(new f.CloudTasksClient({}),t,e,r)}},d=class extends h{static{i(this,"EmulatorTasksClient")}constructor(t,e,r,s){super(new f.CloudTasksClient({sslCreds:R.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:r,...t}}),a.Ok.EMPTY}catch(n){return k(n)&&n.code!==g.Status.ALREADY_EXISTS?(0,a.Err)(l(n)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),a.Ok.EMPTY)}}catch(e){return(0,a.Err)(l(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function N(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${Date.now()}-${e}`}i(N,"createUniqueName");function k(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}i(k,"isRpcError");function l(o){return k(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}i(l,"toTasksError");function O({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:s,logger:n={error:console.error,info:console.info,warn:console.warn}}){n.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let u=t?new d(e,o,n,t):new m(e,o,n);return new Proxy({},{get:i((j,y)=>y==="_manage"?{find:i(c=>u.getTask(c).then(T=>T.unwrap()),"find"),list:i(()=>u.listTasks().then(c=>c.unwrap()),"list")}:{schedule:i(async(c,T)=>{let w=y.toString();await u.upsertQueue(s);let P=new URL(`/trpc/${w}`,r).toString();return n.info({url:P,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await u.createTask(w,{...T,httpRequest:{httpMethod:"POST",url:P,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}i(O,"createTasksClient");
package/dist/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { google } from "@google-cloud/tasks/build/protos";
2
- import type { CreateTRPCProxyClient } from "@trpc/client";
2
+ import type { TRPCClient } from "@trpc/client";
3
3
  import type { AnyRouter } from "@trpc/server";
4
4
  import type { EmulatorConfig, GcpLocation, TaskConfig } from "./tasksClients";
5
5
  export declare function createTasksClient<Router extends AnyRouter>({ queueName, emulator, location, tasksWorkerUrl, queueOptions, logger, }: {
@@ -9,7 +9,7 @@ export declare function createTasksClient<Router extends AnyRouter>({ queueName,
9
9
  tasksWorkerUrl: URL;
10
10
  logger?: Logger;
11
11
  emulator?: false | EmulatorConfig;
12
- }): ScheduleClient<CreateTRPCProxyClient<Router>>;
12
+ }): ScheduleClient<TRPCClient<Router>>;
13
13
  type RemoveNeverKeys<T> = {
14
14
  [K in keyof T as T[K] extends never ? never : K]: T[K];
15
15
  };
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"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"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"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"}
package/dist/client.mjs CHANGED
@@ -1 +1 @@
1
- var C=Object.defineProperty;var a=(o,t)=>C(o,"name",{value:t,configurable:!0});import{CloudTasksClient as P}from"@google-cloud/tasks";import{credentials as R}from"@grpc/grpc-js";import{GoogleAuth as q}from"google-auth-library";import{Status as T}from"google-gax";import{Err as l,Ok as c}from"ts-results-es";var g=class{constructor(t,e,r,n){this.client=t;this.location=e;this.queueName=r;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new q().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(i){if(d(i)&&i.code===T.NOT_FOUND)s="create";else throw i}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:r,...t}})),c.EMPTY}catch(e){return l(p(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return c(r)}catch(t){return l(p(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,E(t)),i=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:n,task:{...e,name:s,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:i}}}}),c(s)}catch(r){return l(p(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return c(e)}catch(e){return d(e)&&e.code===T.NOT_FOUND?c(null):l(p(e))}}},h=class extends g{static{a(this,"RegularTasksClient")}constructor(t,e,r){super(new P({}),t,e,r)}},m=class extends g{static{a(this,"EmulatorTasksClient")}constructor(t,e,r,n){super(new P({sslCreds:R.createInsecure(),servicePath:n.host??"localhost",port:n.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:r,...t}}),c.EMPTY}catch(s){return d(s)&&s.code!==T.ALREADY_EXISTS?l(p(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),c.EMPTY)}}catch(e){return l(p(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function E(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${new Date().getTime()}-${e}`}a(E,"createUniqueName");function d(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(d,"isRpcError");function p(o){return d(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(p,"toTasksError");function K({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:n,logger:s={error:console.error,info:console.info,warn:console.warn}}){s.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let i=t?new m(e,o,s,t):new h(e,o,s);return new Proxy({},{get:a((v,f)=>f==="_manage"?{find:a(u=>i.getTask(u).then(k=>k.unwrap()),"find"),list:a(()=>i.listTasks().then(u=>u.unwrap()),"list")}:{schedule:a(async(u,k)=>{let y=f.toString();await i.upsertQueue(n);let w=new URL(`/trpc/${y}`,r).toString();return s.info({url:w,input:u},"scheduling http request that calls trpc mutation of tasks worker"),(await i.createTask(y,{...k,httpRequest:{httpMethod:"POST",url:w,headers:{"Content-Type":"application/json"},body:u?Buffer.from(JSON.stringify(u)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(K,"createTasksClient");export{K as createTasksClient};
1
+ var R=Object.defineProperty;var a=(o,t)=>R(o,"name",{value:t,configurable:!0});import{CloudTasksClient as P}from"@google-cloud/tasks";import{credentials as C}from"@grpc/grpc-js";import{GoogleAuth as q}from"google-auth-library";import{Status as T}from"google-gax";import{Err as l,Ok as c}from"ts-results-es";var g=class{constructor(t,e,r,n){this.client=t;this.location=e;this.queueName=r;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new q().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(i){if(d(i)&&i.code===T.NOT_FOUND)s="create";else throw i}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:r,...t}})),c.EMPTY}catch(e){return l(p(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return c(r)}catch(t){return l(p(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,E(t)),i=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:n,task:{...e,name:s,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:i}}}}),c(s)}catch(r){return l(p(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return c(e)}catch(e){return d(e)&&e.code===T.NOT_FOUND?c(null):l(p(e))}}},h=class extends g{static{a(this,"RegularTasksClient")}constructor(t,e,r){super(new P({}),t,e,r)}},m=class extends g{static{a(this,"EmulatorTasksClient")}constructor(t,e,r,n){super(new P({sslCreds:C.createInsecure(),servicePath:n.host??"localhost",port:n.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:r,...t}}),c.EMPTY}catch(s){return d(s)&&s.code!==T.ALREADY_EXISTS?l(p(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),c.EMPTY)}}catch(e){return l(p(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function E(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${Date.now()}-${e}`}a(E,"createUniqueName");function d(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(d,"isRpcError");function p(o){return d(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(p,"toTasksError");function K({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:n,logger:s={error:console.error,info:console.info,warn:console.warn}}){s.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let i=t?new m(e,o,s,t):new h(e,o,s);return new Proxy({},{get:a((v,f)=>f==="_manage"?{find:a(u=>i.getTask(u).then(k=>k.unwrap()),"find"),list:a(()=>i.listTasks().then(u=>u.unwrap()),"list")}:{schedule:a(async(u,k)=>{let y=f.toString();await i.upsertQueue(n);let w=new URL(`/trpc/${y}`,r).toString();return s.info({url:w,input:u},"scheduling http request that calls trpc mutation of tasks worker"),(await i.createTask(y,{...k,httpRequest:{httpMethod:"POST",url:w,headers:{"Content-Type":"application/json"},body:u?Buffer.from(JSON.stringify(u)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(K,"createTasksClient");export{K as createTasksClient};
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var I=Object.create;var p=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var O=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var a=(r,t)=>p(r,"name",{value:t,configurable:!0});var A=(r,t)=>{for(var e in t)p(r,e,{get:t[e],enumerable:!0})},R=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of N(t))!L.call(r,s)&&s!==e&&p(r,s,{get:()=>t[s],enumerable:!(o=S(t,s))||o.enumerable});return r};var M=(r,t,e)=>(e=r!=null?I(O(r)):{},R(t||!r||!r.__esModule?p(e,"default",{value:r,enumerable:!0}):e,r)),j=r=>R(p({},"__esModule",{value:!0}),r);var _={};A(_,{createTasksClient:()=>K,createTasksServer:()=>b});module.exports=j(_);var T=require("@google-cloud/tasks"),C=require("@grpc/grpc-js"),q=require("google-auth-library"),g=require("google-gax"),i=require("ts-results-es");var m=class{constructor(t,e,o,s){this.client=t;this.location=e;this.queueName=o;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new q.GoogleAuth().getCredentials().then(n=>n.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,o=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:o,parentPath:s},"upserting tasks queue");let n="create";try{await this.client.getQueue({name:o}),n="update"}catch(u){if(k(u)&&u.code===g.Status.NOT_FOUND)n="create";else throw u}return n==="update"?(this.logger.info({queuePath:o},"updating existing queue"),await this.client.updateQueue({queue:{name:o,...t}})):(this.logger.info({queuePath:o},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:o,...t}})),i.Ok.EMPTY}catch(e){return(0,i.Err)(l(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[o]=await this.client.listTasks({parent:e});return(0,i.Ok)(o)}catch(t){return(0,i.Err)(l(t))}}async createTask(t,e){try{let o=await this.projectIdPromise,s=this.client.queuePath(o,this.location,this.queueName),n=this.client.taskPath(o,this.location,this.queueName,Q(t)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:n,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,i.Ok)(n)}catch(o){return(0,i.Err)(l(o))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,i.Ok)(e)}catch(e){return k(e)&&e.code===g.Status.NOT_FOUND?(0,i.Ok)(null):(0,i.Err)(l(e))}}},h=class extends m{static{a(this,"RegularTasksClient")}constructor(t,e,o){super(new T.CloudTasksClient({}),t,e,o)}},d=class extends m{static{a(this,"EmulatorTasksClient")}constructor(t,e,o,s){super(new T.CloudTasksClient({sslCreds:C.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),t,e,o)}async upsertQueue(t){try{let e=await this.projectIdPromise,o=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:o,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:o,...t}}),i.Ok.EMPTY}catch(n){return k(n)&&n.code!==g.Status.ALREADY_EXISTS?(0,i.Err)(l(n)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),i.Ok.EMPTY)}}catch(e){return(0,i.Err)(l(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function Q(r){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${r}-${new Date().getTime()}-${e}`}a(Q,"createUniqueName");function k(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}a(k,"isRpcError");function l(r){return k(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}a(l,"toTasksError");function K({queueName:r,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:o,queueOptions:s,logger:n={error:console.error,info:console.info,warn:console.warn}}){n.info({queueName:r,emulator:t,location:e,tasksWorkerUrl:o},"creating tasks client");let u=t?new d(e,r,n,t):new h(e,r,n);return new Proxy({},{get:a((U,y)=>y==="_manage"?{find:a(c=>u.getTask(c).then(f=>f.unwrap()),"find"),list:a(()=>u.listTasks().then(c=>c.unwrap()),"list")}:{schedule:a(async(c,f)=>{let w=y.toString();await u.upsertQueue(s);let P=new URL(`/trpc/${w}`,o).toString();return n.info({url:P,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await u.createTask(w,{...f,httpRequest:{httpMethod:"POST",url:P,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(K,"createTasksClient");var v=require("@trpc/server"),E=require("@trpc/server/adapters/express"),x=M(require("express"));function b(r){let t=v.initTRPC.create(),e=r(t),o=(0,x.default)();return o.use("/trpc",(0,E.createExpressMiddleware)({router:e})),{runServer:a(s=>{o.listen({port:s})},"runServer"),router:e}}a(b,"createTasksServer");
1
+ "use strict";var N=Object.create;var p=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var O=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var a=(r,t)=>p(r,"name",{value:t,configurable:!0});var j=(r,t)=>{for(var e in t)p(r,e,{get:t[e],enumerable:!0})},P=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of M(t))!L.call(r,s)&&s!==e&&p(r,s,{get:()=>t[s],enumerable:!(o=A(t,s))||o.enumerable});return r};var Q=(r,t,e)=>(e=r!=null?N(O(r)):{},P(t||!r||!r.__esModule?p(e,"default",{value:r,enumerable:!0}):e,r)),K=r=>P(p({},"__esModule",{value:!0}),r);var G={};j(G,{createExpressTaskServer:()=>I,createExpressTaskServerMiddleware:()=>S,createTasksClient:()=>_,createTasksServer:()=>U});module.exports=K(G);var T=require("@google-cloud/tasks"),C=require("@grpc/grpc-js"),v=require("google-auth-library"),g=require("google-gax"),i=require("ts-results-es");var d=class{constructor(t,e,o,s){this.client=t;this.location=e;this.queueName=o;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new v.GoogleAuth().getCredentials().then(n=>n.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,o=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:o,parentPath:s},"upserting tasks queue");let n="create";try{await this.client.getQueue({name:o}),n="update"}catch(u){if(k(u)&&u.code===g.Status.NOT_FOUND)n="create";else throw u}return n==="update"?(this.logger.info({queuePath:o},"updating existing queue"),await this.client.updateQueue({queue:{name:o,...t}})):(this.logger.info({queuePath:o},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:o,...t}})),i.Ok.EMPTY}catch(e){return(0,i.Err)(l(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[o]=await this.client.listTasks({parent:e});return(0,i.Ok)(o)}catch(t){return(0,i.Err)(l(t))}}async createTask(t,e){try{let o=await this.projectIdPromise,s=this.client.queuePath(o,this.location,this.queueName),n=this.client.taskPath(o,this.location,this.queueName,b(t)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:n,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...e,name:n,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,i.Ok)(n)}catch(o){return(0,i.Err)(l(o))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return(0,i.Ok)(e)}catch(e){return k(e)&&e.code===g.Status.NOT_FOUND?(0,i.Ok)(null):(0,i.Err)(l(e))}}},m=class extends d{static{a(this,"RegularTasksClient")}constructor(t,e,o){super(new T.CloudTasksClient({}),t,e,o)}},h=class extends d{static{a(this,"EmulatorTasksClient")}constructor(t,e,o,s){super(new T.CloudTasksClient({sslCreds:C.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),t,e,o)}async upsertQueue(t){try{let e=await this.projectIdPromise,o=this.client.queuePath(e,this.location,this.queueName),s=this.client.locationPath(e,this.location);this.logger.info({queuePath:o,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:o,...t}}),i.Ok.EMPTY}catch(n){return k(n)&&n.code!==g.Status.ALREADY_EXISTS?(0,i.Err)(l(n)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),i.Ok.EMPTY)}}catch(e){return(0,i.Err)(l(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function b(r){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${r}-${Date.now()}-${e}`}a(b,"createUniqueName");function k(r){return r instanceof Error&&"code"in r&&Number.isInteger(r.code)}a(k,"isRpcError");function l(r){return k(r)?{...r,type:"rpc"}:{type:"unknown",error:r}}a(l,"toTasksError");function _({queueName:r,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:o,queueOptions:s,logger:n={error:console.error,info:console.info,warn:console.warn}}){n.info({queueName:r,emulator:t,location:e,tasksWorkerUrl:o},"creating tasks client");let u=t?new h(e,r,n,t):new m(e,r,n);return new Proxy({},{get:a((D,y)=>y==="_manage"?{find:a(c=>u.getTask(c).then(f=>f.unwrap()),"find"),list:a(()=>u.listTasks().then(c=>c.unwrap()),"list")}:{schedule:a(async(c,f)=>{let w=y.toString();await u.upsertQueue(s);let R=new URL(`/trpc/${w}`,o).toString();return n.info({url:R,input:c},"scheduling http request that calls trpc mutation of tasks worker"),(await u.createTask(w,{...f,httpRequest:{httpMethod:"POST",url:R,headers:{"Content-Type":"application/json"},body:c?Buffer.from(JSON.stringify(c)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(_,"createTasksClient");var x=require("@trpc/server"),q=require("@trpc/server/adapters/express"),E=Q(require("express"));var S=a(r=>{let t=x.initTRPC.create(),e=r.createRouter(t);return{expressMiddleware:(0,q.createExpressMiddleware)({router:e}),router:e}},"createExpressTaskServerMiddleware");function I(r,t){let e=t??(0,E.default)(),{expressMiddleware:o,router:s}=S(r);return e.use("/trpc",o),{runServer:a(n=>{e.listen({port:n})},"runServer"),router:s}}a(I,"createExpressTaskServer");var U=I;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var R=Object.defineProperty;var a=(o,t)=>R(o,"name",{value:t,configurable:!0});import{CloudTasksClient as P}from"@google-cloud/tasks";import{credentials as C}from"@grpc/grpc-js";import{GoogleAuth as q}from"google-auth-library";import{Status as f}from"google-gax";import{Err as l,Ok as c}from"ts-results-es";var g=class{constructor(t,e,r,n){this.client=t;this.location=e;this.queueName=r;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new q().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(i){if(d(i)&&i.code===f.NOT_FOUND)s="create";else throw i}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:r,...t}})),c.EMPTY}catch(e){return l(p(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return c(r)}catch(t){return l(p(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,v(t)),i=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:n,task:{...e,name:s,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:i}}}}),c(s)}catch(r){return l(p(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return c(e)}catch(e){return d(e)&&e.code===f.NOT_FOUND?c(null):l(p(e))}}},m=class extends g{static{a(this,"RegularTasksClient")}constructor(t,e,r){super(new P({}),t,e,r)}},h=class extends g{static{a(this,"EmulatorTasksClient")}constructor(t,e,r,n){super(new P({sslCreds:C.createInsecure(),servicePath:n.host??"localhost",port:n.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),n=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:r,...t}}),c.EMPTY}catch(s){return d(s)&&s.code!==f.ALREADY_EXISTS?l(p(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),c.EMPTY)}}catch(e){return l(p(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function v(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${new Date().getTime()}-${e}`}a(v,"createUniqueName");function d(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(d,"isRpcError");function p(o){return d(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(p,"toTasksError");function U({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:n,logger:s={error:console.error,info:console.info,warn:console.warn}}){s.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let i=t?new h(e,o,s,t):new m(e,o,s);return new Proxy({},{get:a((S,T)=>T==="_manage"?{find:a(u=>i.getTask(u).then(k=>k.unwrap()),"find"),list:a(()=>i.listTasks().then(u=>u.unwrap()),"list")}:{schedule:a(async(u,k)=>{let y=T.toString();await i.upsertQueue(n);let w=new URL(`/trpc/${y}`,r).toString();return s.info({url:w,input:u},"scheduling http request that calls trpc mutation of tasks worker"),(await i.createTask(y,{...k,httpRequest:{httpMethod:"POST",url:w,headers:{"Content-Type":"application/json"},body:u?Buffer.from(JSON.stringify(u)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}a(U,"createTasksClient");import{initTRPC as E}from"@trpc/server";import{createExpressMiddleware as x}from"@trpc/server/adapters/express";import I from"express";function B(o){let t=E.create(),e=o(t),r=I();return r.use("/trpc",x({router:e})),{runServer:a(n=>{r.listen({port:n})},"runServer"),router:e}}a(B,"createTasksServer");export{U as createTasksClient,B as createTasksServer};
1
+ var P=Object.defineProperty;var n=(o,t)=>P(o,"name",{value:t,configurable:!0});import{CloudTasksClient as R}from"@google-cloud/tasks";import{credentials as C}from"@grpc/grpc-js";import{GoogleAuth as v}from"google-auth-library";import{Status as f}from"google-gax";import{Err as l,Ok as c}from"ts-results-es";var g=class{constructor(t,e,r,a){this.client=t;this.location=e;this.queueName=r;this.logger=a;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new v().getCredentials().then(s=>s.client_email??null)}static{n(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),a=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:a},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(i){if(h(i)&&i.code===f.NOT_FOUND)s="create";else throw i}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...t}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:a,queue:{name:r,...t}})),c.EMPTY}catch(e){return l(p(e))}}async listTasks(){try{let t=await this.projectIdPromise,e=this.client.queuePath(t,this.location,this.queueName),[r]=await this.client.listTasks({parent:e});return c(r)}catch(t){return l(p(t))}}async createTask(t,e){try{let r=await this.projectIdPromise,a=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,x(t)),i=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:a,taskPath:s,url:e?.httpRequest?.url,payload:e?.httpRequest?.body,serviceAccountEmail:i},"scheduling task"),await this.client.createTask({parent:a,task:{...e,name:s,httpRequest:{...e?.httpRequest,oidcToken:{serviceAccountEmail:i}}}}),c(s)}catch(r){return l(p(r))}}async getTask(t){try{let[e]=await this.client.getTask({name:t});return c(e)}catch(e){return h(e)&&e.code===f.NOT_FOUND?c(null):l(p(e))}}},d=class extends g{static{n(this,"RegularTasksClient")}constructor(t,e,r){super(new R({}),t,e,r)}},m=class extends g{static{n(this,"EmulatorTasksClient")}constructor(t,e,r,a){super(new R({sslCreds:C.createInsecure(),servicePath:a.host??"localhost",port:a.port}),t,e,r)}async upsertQueue(t){try{let e=await this.projectIdPromise,r=this.client.queuePath(e,this.location,this.queueName),a=this.client.locationPath(e,this.location);this.logger.info({queuePath:r,parentPath:a},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:a,queue:{name:r,...t}}),c.EMPTY}catch(s){return h(s)&&s.code!==f.ALREADY_EXISTS?l(p(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),c.EMPTY)}}catch(e){return l(p(e))}}async createTask(t,e){return super.createTask(t,{...e,httpRequest:{...e?.httpRequest,url:e?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function x(o){let t=new Uint8Array(3);crypto.getRandomValues(t);let e=Buffer.from(t).toString("hex");return`${o}-${Date.now()}-${e}`}n(x,"createUniqueName");function h(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}n(h,"isRpcError");function p(o){return h(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}n(p,"toTasksError");function D({queueName:o,emulator:t=!1,location:e="europe-west6",tasksWorkerUrl:r,queueOptions:a,logger:s={error:console.error,info:console.info,warn:console.warn}}){s.info({queueName:o,emulator:t,location:e,tasksWorkerUrl:r},"creating tasks client");let i=t?new m(e,o,s,t):new d(e,o,s);return new Proxy({},{get:n((A,T)=>T==="_manage"?{find:n(u=>i.getTask(u).then(k=>k.unwrap()),"find"),list:n(()=>i.listTasks().then(u=>u.unwrap()),"list")}:{schedule:n(async(u,k)=>{let y=T.toString();await i.upsertQueue(a);let w=new URL(`/trpc/${y}`,r).toString();return s.info({url:w,input:u},"scheduling http request that calls trpc mutation of tasks worker"),(await i.createTask(y,{...k,httpRequest:{httpMethod:"POST",url:w,headers:{"Content-Type":"application/json"},body:u?Buffer.from(JSON.stringify(u)).toString("base64"):null}})).unwrap()},"schedule")},"get")})}n(D,"createTasksClient");import{initTRPC as q}from"@trpc/server";import{createExpressMiddleware as E}from"@trpc/server/adapters/express";import S from"express";var I=n(o=>{let t=q.create(),e=o.createRouter(t);return{expressMiddleware:E({router:e}),router:e}},"createExpressTaskServerMiddleware");function N(o,t){let e=t??S(),{expressMiddleware:r,router:a}=I(o);return e.use("/trpc",r),{runServer:n(s=>{e.listen({port:s})},"runServer"),router:a}}n(N,"createExpressTaskServer");var V=N;export{N as createExpressTaskServer,I as createExpressTaskServerMiddleware,D as createTasksClient,V as createTasksServer};
package/dist/server.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var a=Object.create;var p=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var y=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty;var s=(e,r)=>p(e,"name",{value:r,configurable:!0});var T=(e,r)=>{for(var t in r)p(e,t,{get:r[t],enumerable:!0})},i=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of f(r))!x.call(e,o)&&o!==t&&p(e,o,{get:()=>r[o],enumerable:!(n=R(r,o))||n.enumerable});return e};var d=(e,r,t)=>(t=e!=null?a(y(e)):{},i(r||!e||!e.__esModule?p(t,"default",{value:e,enumerable:!0}):t,e)),v=e=>i(p({},"__esModule",{value:!0}),e);var A={};T(A,{createTasksServer:()=>l});module.exports=v(A);var u=require("@trpc/server"),c=require("@trpc/server/adapters/express"),m=d(require("express"));function l(e){let r=u.initTRPC.create(),t=e(r),n=(0,m.default)();return n.use("/trpc",(0,c.createExpressMiddleware)({router:t})),{runServer:s(o=>{n.listen({port:o})},"runServer"),router:t}}s(l,"createTasksServer");
1
+ "use strict";var f=Object.create;var n=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,y=Object.prototype.hasOwnProperty;var p=(e,r)=>n(e,"name",{value:r,configurable:!0});var l=(e,r)=>{for(var t in r)n(e,t,{get:r[t],enumerable:!0})},a=(e,r,t,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of T(r))!y.call(e,o)&&o!==t&&n(e,o,{get:()=>r[o],enumerable:!(s=m(r,o))||s.enumerable});return e};var S=(e,r,t)=>(t=e!=null?f(v(e)):{},a(r||!e||!e.__esModule?n(t,"default",{value:e,enumerable:!0}):t,e)),k=e=>a(n({},"__esModule",{value:!0}),e);var A={};l(A,{createExpressTaskServer:()=>R,createExpressTaskServerMiddleware:()=>x,createTasksServer:()=>w});module.exports=k(A);var i=require("@trpc/server"),c=require("@trpc/server/adapters/express"),u=S(require("express"));var x=p(e=>{let r=i.initTRPC.create(),t=e.createRouter(r);return{expressMiddleware:(0,c.createExpressMiddleware)({router:t}),router:t}},"createExpressTaskServerMiddleware");function R(e,r){let t=r??(0,u.default)(),{expressMiddleware:s,router:o}=x(e);return t.use("/trpc",s),{runServer:p(d=>{t.listen({port:d})},"runServer"),router:o}}p(R,"createExpressTaskServer");var w=R;
package/dist/server.d.ts CHANGED
@@ -1,7 +1,20 @@
1
1
  import type { AnyRouter } from "@trpc/server";
2
2
  import { initTRPC } from "@trpc/server";
3
- export declare function createTasksServer<Router extends AnyRouter>(createRouter: (t: ReturnType<(typeof initTRPC)["create"]>) => Router): {
3
+ import express from "express";
4
+ type TaskServerConfig<Router extends AnyRouter> = {
5
+ createRouter: (t: ReturnType<(typeof initTRPC)["create"]>) => Router;
6
+ };
7
+ export declare const createExpressTaskServerMiddleware: (config: TaskServerConfig<AnyRouter>) => {
8
+ expressMiddleware: express.Handler;
9
+ router: AnyRouter;
10
+ };
11
+ export declare function createExpressTaskServer<Router extends AnyRouter>(config: TaskServerConfig<Router>, existingApp?: express.Application): {
4
12
  runServer: (port: number) => void;
5
- router: Router;
13
+ router: AnyRouter;
6
14
  };
15
+ /**
16
+ * @deprecated Use `createExpressTaskServer` instead
17
+ */
18
+ export declare const createTasksServer: typeof createExpressTaskServer;
19
+ export {};
7
20
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAIxC,wBAAgB,iBAAiB,CAAC,MAAM,SAAS,SAAS,EACxD,YAAY,EAAE,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,OAAO,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC,KAAK,MAAM;sBAShD,MAAM;;EAK3B"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,KAAK,gBAAgB,CAAC,MAAM,SAAS,SAAS,IAAI;IAChD,YAAY,EAAE,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,OAAO,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC,KAAK,MAAM,CAAC;CACtE,CAAC;AAEF,eAAO,MAAM,iCAAiC,GAC5C,QAAQ,gBAAgB,CAAC,SAAS,CAAC;;;CASpC,CAAC;AACF,wBAAgB,uBAAuB,CAAC,MAAM,SAAS,SAAS,EAC9D,MAAM,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAChC,WAAW,CAAC,EAAE,OAAO,CAAC,WAAW;sBAQb,MAAM;;EAK3B;AAED;;GAEG;AACH,eAAO,MAAM,iBAAiB,gCAA0B,CAAC"}
package/dist/server.mjs CHANGED
@@ -1 +1 @@
1
- var s=Object.defineProperty;var t=(e,r)=>s(e,"name",{value:r,configurable:!0});import{initTRPC as i}from"@trpc/server";import{createExpressMiddleware as u}from"@trpc/server/adapters/express";import c from"express";function y(e){let r=i.create(),o=e(r),n=c();return n.use("/trpc",u({router:o})),{runServer:t(p=>{n.listen({port:p})},"runServer"),router:o}}t(y,"createTasksServer");export{y as createTasksServer};
1
+ var a=Object.defineProperty;var o=(e,r)=>a(e,"name",{value:r,configurable:!0});import{initTRPC as i}from"@trpc/server";import{createExpressMiddleware as c}from"@trpc/server/adapters/express";import u from"express";var x=o(e=>{let r=i.create(),t=e.createRouter(r);return{expressMiddleware:c({router:t}),router:t}},"createExpressTaskServerMiddleware");function R(e,r){let t=r??u(),{expressMiddleware:s,router:n}=x(e);return t.use("/trpc",s),{runServer:o(p=>{t.listen({port:p})},"runServer"),router:n}}o(R,"createExpressTaskServer");var v=R;export{R as createExpressTaskServer,x as createExpressTaskServerMiddleware,v as createTasksServer};
@@ -1 +1 @@
1
- "use strict";var l=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var i=(o,e)=>l(o,"name",{value:e,configurable:!0});var q=(o,e)=>{for(var t in e)l(o,t,{get:e[t],enumerable:!0})},E=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of f(e))!w.call(o,s)&&s!==t&&l(o,s,{get:()=>e[s],enumerable:!(r=T(e,s))||r.enumerable});return o};var I=o=>E(l({},"__esModule",{value:!0}),o);var v={};q(v,{EmulatorTasksClient:()=>d,RegularTasksClient:()=>m});module.exports=I(v);var k=require("@google-cloud/tasks"),y=require("@grpc/grpc-js"),P=require("google-auth-library"),p=require("google-gax"),n=require("ts-results-es");var h=class{constructor(e,t,r,s){this.client=e;this.location=t;this.queueName=r;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new P.GoogleAuth().getCredentials().then(a=>a.client_email??null)}static{i(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),s=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting tasks queue");let a="create";try{await this.client.getQueue({name:r}),a="update"}catch(u){if(g(u)&&u.code===p.Status.NOT_FOUND)a="create";else throw u}return a==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...e}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:r,...e}})),n.Ok.EMPTY}catch(t){return(0,n.Err)(c(t))}}async listTasks(){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),[r]=await this.client.listTasks({parent:t});return(0,n.Ok)(r)}catch(e){return(0,n.Err)(c(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),a=this.client.taskPath(r,this.location,this.queueName,R(e)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:a,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...t,name:a,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,n.Ok)(a)}catch(r){return(0,n.Err)(c(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return(0,n.Ok)(t)}catch(t){return g(t)&&t.code===p.Status.NOT_FOUND?(0,n.Ok)(null):(0,n.Err)(c(t))}}},m=class extends h{static{i(this,"RegularTasksClient")}constructor(e,t,r){super(new k.CloudTasksClient({}),e,t,r)}},d=class extends h{static{i(this,"EmulatorTasksClient")}constructor(e,t,r,s){super(new k.CloudTasksClient({sslCreds:y.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),e,t,r)}async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),s=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:r,...e}}),n.Ok.EMPTY}catch(a){return g(a)&&a.code!==p.Status.ALREADY_EXISTS?(0,n.Err)(c(a)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),n.Ok.EMPTY)}}catch(t){return(0,n.Err)(c(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function R(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${new Date().getTime()}-${t}`}i(R,"createUniqueName");function g(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}i(g,"isRpcError");function c(o){return g(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}i(c,"toTasksError");
1
+ "use strict";var l=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var i=(o,e)=>l(o,"name",{value:e,configurable:!0});var q=(o,e)=>{for(var t in e)l(o,t,{get:e[t],enumerable:!0})},E=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of f(e))!w.call(o,s)&&s!==t&&l(o,s,{get:()=>e[s],enumerable:!(r=T(e,s))||r.enumerable});return o};var I=o=>E(l({},"__esModule",{value:!0}),o);var v={};q(v,{EmulatorTasksClient:()=>d,RegularTasksClient:()=>m});module.exports=I(v);var k=require("@google-cloud/tasks"),y=require("@grpc/grpc-js"),P=require("google-auth-library"),p=require("google-gax"),n=require("ts-results-es");var h=class{constructor(e,t,r,s){this.client=e;this.location=t;this.queueName=r;this.logger=s;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new P.GoogleAuth().getCredentials().then(a=>a.client_email??null)}static{i(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),s=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting tasks queue");let a="create";try{await this.client.getQueue({name:r}),a="update"}catch(u){if(g(u)&&u.code===p.Status.NOT_FOUND)a="create";else throw u}return a==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...e}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:s,queue:{name:r,...e}})),n.Ok.EMPTY}catch(t){return(0,n.Err)(c(t))}}async listTasks(){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),[r]=await this.client.listTasks({parent:t});return(0,n.Ok)(r)}catch(e){return(0,n.Err)(c(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,s=this.client.queuePath(r,this.location,this.queueName),a=this.client.taskPath(r,this.location,this.queueName,R(e)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:s,taskPath:a,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:s,task:{...t,name:a,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),(0,n.Ok)(a)}catch(r){return(0,n.Err)(c(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return(0,n.Ok)(t)}catch(t){return g(t)&&t.code===p.Status.NOT_FOUND?(0,n.Ok)(null):(0,n.Err)(c(t))}}},m=class extends h{static{i(this,"RegularTasksClient")}constructor(e,t,r){super(new k.CloudTasksClient({}),e,t,r)}},d=class extends h{static{i(this,"EmulatorTasksClient")}constructor(e,t,r,s){super(new k.CloudTasksClient({sslCreds:y.credentials.createInsecure(),servicePath:s.host??"localhost",port:s.port}),e,t,r)}async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),s=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:s},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:s,queue:{name:r,...e}}),n.Ok.EMPTY}catch(a){return g(a)&&a.code!==p.Status.ALREADY_EXISTS?(0,n.Err)(c(a)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),n.Ok.EMPTY)}}catch(t){return(0,n.Err)(c(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function R(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${Date.now()}-${t}`}i(R,"createUniqueName");function g(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}i(g,"isRpcError");function c(o){return g(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}i(c,"toTasksError");
@@ -1 +1 @@
1
- var y=Object.defineProperty;var a=(o,e)=>y(o,"name",{value:e,configurable:!0});import{CloudTasksClient as k}from"@google-cloud/tasks";import{credentials as P}from"@grpc/grpc-js";import{GoogleAuth as T}from"google-auth-library";import{Status as g}from"google-gax";import{Err as c,Ok as i}from"ts-results-es";var p=class{constructor(e,t,r,n){this.client=e;this.location=t;this.queueName=r;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new T().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),n=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(u){if(h(u)&&u.code===g.NOT_FOUND)s="create";else throw u}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...e}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:r,...e}})),i.EMPTY}catch(t){return c(l(t))}}async listTasks(){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),[r]=await this.client.listTasks({parent:t});return i(r)}catch(e){return c(l(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,f(e)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:n,task:{...t,name:s,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),i(s)}catch(r){return c(l(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return i(t)}catch(t){return h(t)&&t.code===g.NOT_FOUND?i(null):c(l(t))}}},m=class extends p{static{a(this,"RegularTasksClient")}constructor(e,t,r){super(new k({}),e,t,r)}},d=class extends p{static{a(this,"EmulatorTasksClient")}constructor(e,t,r,n){super(new k({sslCreds:P.createInsecure(),servicePath:n.host??"localhost",port:n.port}),e,t,r)}async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),n=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:r,...e}}),i.EMPTY}catch(s){return h(s)&&s.code!==g.ALREADY_EXISTS?c(l(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),i.EMPTY)}}catch(t){return c(l(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function f(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${new Date().getTime()}-${t}`}a(f,"createUniqueName");function h(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(h,"isRpcError");function l(o){return h(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(l,"toTasksError");export{d as EmulatorTasksClient,m as RegularTasksClient};
1
+ var y=Object.defineProperty;var a=(o,e)=>y(o,"name",{value:e,configurable:!0});import{CloudTasksClient as k}from"@google-cloud/tasks";import{credentials as P}from"@grpc/grpc-js";import{GoogleAuth as T}from"google-auth-library";import{Status as g}from"google-gax";import{Err as c,Ok as i}from"ts-results-es";var p=class{constructor(e,t,r,n){this.client=e;this.location=t;this.queueName=r;this.logger=n;this.projectIdPromise=this.client.getProjectId(),this.serviceAccountEmailPromise=new T().getCredentials().then(s=>s.client_email??null)}static{a(this,"CommonTasksClient")}serviceAccountEmailPromise;projectIdPromise;async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),n=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting tasks queue");let s="create";try{await this.client.getQueue({name:r}),s="update"}catch(u){if(h(u)&&u.code===g.NOT_FOUND)s="create";else throw u}return s==="update"?(this.logger.info({queuePath:r},"updating existing queue"),await this.client.updateQueue({queue:{name:r,...e}})):(this.logger.info({queuePath:r},"creating new queue"),await this.client.createQueue({parent:n,queue:{name:r,...e}})),i.EMPTY}catch(t){return c(l(t))}}async listTasks(){try{let e=await this.projectIdPromise,t=this.client.queuePath(e,this.location,this.queueName),[r]=await this.client.listTasks({parent:t});return i(r)}catch(e){return c(l(e))}}async createTask(e,t){try{let r=await this.projectIdPromise,n=this.client.queuePath(r,this.location,this.queueName),s=this.client.taskPath(r,this.location,this.queueName,f(e)),u=await this.serviceAccountEmailPromise;return this.logger.info({queuePath:n,taskPath:s,url:t?.httpRequest?.url,payload:t?.httpRequest?.body,serviceAccountEmail:u},"scheduling task"),await this.client.createTask({parent:n,task:{...t,name:s,httpRequest:{...t?.httpRequest,oidcToken:{serviceAccountEmail:u}}}}),i(s)}catch(r){return c(l(r))}}async getTask(e){try{let[t]=await this.client.getTask({name:e});return i(t)}catch(t){return h(t)&&t.code===g.NOT_FOUND?i(null):c(l(t))}}},m=class extends p{static{a(this,"RegularTasksClient")}constructor(e,t,r){super(new k({}),e,t,r)}},d=class extends p{static{a(this,"EmulatorTasksClient")}constructor(e,t,r,n){super(new k({sslCreds:P.createInsecure(),servicePath:n.host??"localhost",port:n.port}),e,t,r)}async upsertQueue(e){try{let t=await this.projectIdPromise,r=this.client.queuePath(t,this.location,this.queueName),n=this.client.locationPath(t,this.location);this.logger.info({queuePath:r,parentPath:n},"upserting simulator tasks queue");try{return await this.client.createQueue({parent:n,queue:{name:r,...e}}),i.EMPTY}catch(s){return h(s)&&s.code!==g.ALREADY_EXISTS?c(l(s)):(this.logger.warn({},"dev warning: the queue was not updated due to the emulator not supporting update"),this.logger.warn({},"if you need to update queue options, restart the emulator"),i.EMPTY)}}catch(t){return c(l(t))}}async createTask(e,t){return super.createTask(e,{...t,httpRequest:{...t?.httpRequest,url:t?.httpRequest?.url?.replace("//localhost","//host.containers.internal")}})}};function f(o){let e=new Uint8Array(3);crypto.getRandomValues(e);let t=Buffer.from(e).toString("hex");return`${o}-${Date.now()}-${t}`}a(f,"createUniqueName");function h(o){return o instanceof Error&&"code"in o&&Number.isInteger(o.code)}a(h,"isRpcError");function l(o){return h(o)?{...o,type:"rpc"}:{type:"unknown",error:o}}a(l,"toTasksError");export{d as EmulatorTasksClient,m as RegularTasksClient};
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://git.panter.ch/open-source/cloud-tasks.git"
6
6
  },
7
- "version": "1.1.4",
7
+ "version": "2.0.0",
8
8
  "sideEffects": false,
9
9
  "exports": {
10
10
  ".": {
@@ -47,18 +47,18 @@
47
47
  "test": "echo 'todo: write tests'"
48
48
  },
49
49
  "dependencies": {
50
- "@google-cloud/tasks": "^5.5.2",
51
- "@trpc/client": "^10.45.2",
52
- "@trpc/server": "^10.45.2",
53
- "express": "^4.21.2",
54
- "ts-results-es": "^4.2.0"
50
+ "@google-cloud/tasks": "^6.2.1",
51
+ "@trpc/client": "^11.8.1",
52
+ "@trpc/server": "^11.8.1",
53
+ "express": "^5",
54
+ "ts-results-es": "^6.0.0"
55
55
  },
56
56
  "devDependencies": {
57
- "@biomejs/biome": "1.9.4",
58
- "@types/express": "^4.17.21",
59
- "esbuild": "^0.24.2",
60
- "prettier": "^3.4.2",
61
- "typescript": "5.7.3"
57
+ "@biomejs/biome": "2.3.10",
58
+ "@types/express": "^5.0.6",
59
+ "esbuild": "^0.27.2",
60
+ "prettier": "^3.7.4",
61
+ "typescript": "^5.9.3"
62
62
  },
63
63
  "packageManager": "yarn@4.6.0"
64
64
  }