@@ -88,22 +88,72 @@ export interface TaskContext {
8888 * The result of decoding and verifying an ODIC token.
8989 */
9090 auth ?: AuthData ;
91+
92+ /**
93+ * The name of the queue.
94+ * Populated via the `X-CloudTasks-QueueName` header.
95+ */
96+ queueName : string ;
97+
98+ /**
99+ * The "short" name of the task, or, if no name was specified at creation, a unique
100+ * system-generated id.
101+ * This is the my-task-id value in the complete task name, ie, task_name =
102+ * projects/my-project-id/locations/my-location/queues/my-queue-id/tasks/my-task-id.
103+ * Populated via the `X-CloudTasks-TaskName` header.
104+ */
105+ id : string ;
106+
107+ /**
108+ * The number of times this task has been retried.
109+ * For the first attempt, this value is 0. This number includes attempts where the task failed
110+ * due to 5XX error codes and never reached the execution phase.
111+ * Populated via the `X-CloudTasks-TaskRetryCount` header.
112+ */
113+ retryCount : number ;
114+
115+ /**
116+ * The total number of times that the task has received a response from the handler.
117+ * Since Cloud Tasks deletes the task once a successful response has been received, all
118+ * previous handler responses were failures. This number does not include failures due to 5XX
119+ * error codes.
120+ * Populated via the `X-CloudTasks-TaskExecutionCount` header.
121+ */
122+ executionCount : number ;
123+
124+ /**
125+ * The schedule time of the task, as an RFC 3339 string in UTC time zone.
126+ * Populated via the `X-CloudTasks-TaskETA` header, which uses seconds since January 1 1970.
127+ */
128+ scheduledTime : string ;
129+
130+ /**
131+ * The HTTP response code from the previous retry.
132+ * Populated via the `X-CloudTasks-TaskPreviousResponse` header
133+ */
134+ previousResponse ?: number ;
135+
136+ /**
137+ * The reason for retrying the task.
138+ * Populated via the `X-CloudTasks-TaskRetryReason` header.
139+ */
140+ retryReason ?: string ;
141+
142+ /**
143+ * Raw request headers.
144+ */
145+ headers ?: Record < string , string > ;
91146}
92147
93148/**
94- * The request used to call a Task Queue function.
149+ * The request used to call a task queue function.
95150 */
96- export interface Request < T = any > {
151+ export type Request < T = any > = TaskContext & {
97152 /**
98153 * The parameters used by a client when calling this function.
99154 */
100155 data : T ;
101-
102- /**
103- * The result of decoding and verifying an ODIC token.
104- */
105- auth ?: AuthData ;
106- }
156+ } ;
107157
108158type v1TaskHandler = ( data : any , context : TaskContext ) => void | Promise < void > ;
109159type v2TaskHandler < Req > = ( request : Request < Req > ) => void | Promise < void > ;
@@ -119,7 +169,30 @@ export function onDispatchHandler<Req = any>(
119169 throw new https . HttpsError ( "invalid-argument" , "Bad Request" ) ;
120170 }
121171
122- const context : TaskContext = { } ;
172+ const headers : Record < string , string > = { } ;
173+ for ( const [ key , value ] of Object . entries ( req . headers ) ) {
174+ if ( ! Array . isArray ( value ) ) {
175+ headers [ key ] = value ;
176+ }
177+ }
178+
179+ const context : TaskContext = {
180+ queueName : req . header ( "X-CloudTasks-QueueName" ) ,
181+ id : req . header ( "X-CloudTasks-TaskName" ) ,
182+ retryCount : req . header ( "X-CloudTasks-TaskRetryCount" )
183+ ? Number ( req . header ( "X-CloudTasks-TaskRetryCount" ) )
184+ : undefined ,
185+ executionCount : req . header ( "X-CloudTasks-TaskExecutionCount" )
186+ ? Number ( req . header ( "X-CloudTasks-TaskExecutionCount" ) )
187+ : undefined ,
188+ scheduledTime : req . header ( "X-CloudTasks-TaskETA" ) ,
189+ previousResponse : req . header ( "X-CloudTasks-TaskPreviousResponse" )
190+ ? Number ( req . header ( "X-CloudTasks-TaskPreviousResponse" ) )
191+ : undefined ,
192+ retryReason : req . header ( "X-CloudTasks-TaskRetryReason" ) ,
193+ headers,
194+ } ;
195+
123196 if ( ! process . env . FUNCTIONS_EMULATOR ) {
124197 const authHeader = req . header ( "Authorization" ) || "" ;
125198 const token = authHeader . match ( / ^ B e a r e r ( .* ) $ / ) ?. [ 1 ] ;
0 commit comments