Quick question / note. I have implemented a state ...
# pact-js
b
Quick question / note. I have implemented a state handler for a parameterized provider state like this:
Copy code
'an address with ID {addressId} does not exist': async (params?: { [name: string]: string }) => {
  const addressId = params?.['addressId'];

  if (!addressId) throw new Error('addressId param is required');

  removeAddress(addressId);
},
This works, and I see that the
params
object contains the proper values. However, VS Code tells me:
Copy code
Type '(params?: { [name: string]: string; }) => Promise<void>' is not assignable to type 'StateHandler & ((state: string, params?: { [name: string]: string; } | undefined) => Promise<unknown>)'.
  Type '(params?: { [name: string]: string; }) => Promise<void>' is not assignable to type 'StateFuncWithSetup & ((state: string, params?: { [name: string]: string; } | undefined) => Promise<unknown>)'.
    Type '(params?: { [name: string]: string; }) => Promise<void>' is not assignable to type '(state: string, params?: { [name: string]: string; } | undefined) => Promise<unknown>'.
      Types of parameters 'params' and 'state' are incompatible.
        Type 'string' is not assignable to type '{ [name: string]: string; }'.
If I update the handler signature to
Copy code
'an address with ID {addressId} does not exist': async (state: string, params?: { [name: string]: string }) => {
state
obviously now contains the provider state parameters, and
params
is
undefined
. I'm not a TS expert but doesn't that mean there's something wrong with the type definitions? Or am I missing an explicit type definition package in my project maybe? All I'm using is
"@pact-foundation/pact": "^16.5.0"
.
👋 1
m
Hey Bas! Sorry, long weekend here in Aus and just catching up. Let me see if I can figure it out (aren’t TS errors fun to read 😉 )
state
obviously now contains the provider state parameters, and
params
is
undefined
. I’m not a TS expert but doesn’t that mean there’s something wrong with the type definitions? Or am I missing an explicit type definition package in my project maybe? All I’m using is
"@pact-foundation/pact": "^16.5.0"
.
types are exported in this package, so you shouldn’t need to import any additional ones. We did recently update a type definition, but I think that was for messages. But it’s possible it’s a type def problem
b
Cheers Matt!
m
This is one of those Pact.js TS typing rough edges around parameterized provider states. Check out @seontechnologies/pactjs-utils it wraps the verifier setup and exports cleaner helper types for state handlers, so you can keep the handler in the shape Pact actually gives you at runtime:
Copy code
import {
    buildVerifierOptions,
    type StateHandlers,
  } from '@seontechnologies/pactjs-utils';

  const stateHandlers: StateHandlers = {
    'an address with ID {addressId} does not exist': async (params) => {
      const addressId = params?.addressId;
      if (!addressId) throw new Error('addressId param is required');

      removeAddress(addressId);
    },
  }

  const opts = buildVerifierOptions({
    provider: 'your-provider',
    port: '3000',
    includeMainAndDeployed: true,
    stateHandlers,
  })
It also helps remove a bunch of the usual Pact verifier boilerplate
thankyou 1
b
Thanks, @Murat Ozcan !
m
thanks Murat, I definitely got sidetracked here.
👍 1