Skip to main content

@krakentech/blueprint-utils

This is our package of reusable utilities that we've developed for working with the Kraken API. You can install this package via npm with an organisation token - please get in touch if you need one.

API reference​

getFirstGraphQLErrorCode​

A helper function that returns the first error code from an array of Apollo GraphQL errors.

Import​

import { getFirstGraphQLErrorCode } from '@krakentech/blueprint-utils';

Usage​

import type { ApolloError } from '@apollo/client';

const errors: ApolloError[] = [
{
extensions: {
errorCode: 'KT-1111',
},
},
{
extensions: {
errorCode: 'KT-2222',
},
},
];

const firstErrorCode = getFirstGraphQLErrorCode<ApolloError>(errors);

Args​

NameDescriptionTypeDefaultRequired
errorsArray of errors from which to get the firstT[]true

useApolloPagination​

A hook to assist with paginating data from Apollo, which while not used in Blueprint anymore, was used in previous versions.

Import​

import { useApolloPagination } from '@krakentech/blueprint-utils/client';

Usage​

import { useApolloPagination, mergeResults } from '@krakentech/blueprint-utils/client';

const { data, loading, error, fetchMore } = useQuery(QUERY, {
variables: { accountNumber },
});

const { onLoadMore, hasNextPage } = useApolloPagination({
fetchMore,
pageInfo: data.pageInfo,
mergeData: mergeResults,
});

Args​

NameDescriptionTypeDefaultRequired
fetchMoreThe function that fetches more pages using the apollo clientFetchMoreCallbacktrue
pageInfoPageInfotrue
mergeDataThe function to merge new data into old dataMergeResultsCallbacktrue
variablesAny updated variablesQueryVariablesfalse

Returns​

NameTypeDescriptionExample
onLoadMoreFunction
hasNextPageboolean

Measurements​

See Measurements

Currency​

Here are several utils to format currency amounts.

createFormatCurrencyUtils​

Creates a format currency utility object with the specified locale and currency.

Import​

import { createFormatCurrencyUtils } from '@krakentech/blueprint-utils';

Usage​

import { createFormatCurrencyUtils } from '@krakentech/blueprint-utils';

const { formatCurrency, formatCurrencyToMajorUnit } = createFormatCurrencyUtils(
{ locale: 'en', currency: 'GBP' }
);

formatCurrency({ amount: 1.0 }); // 'Β£1.00'
formatCurrency({ amount: 5.0, roundAmount: true }); // 'Β£5'


formatCurrencyToMajorUnit({ amount: 188 })) // 'Β£1.88'
formatCurrencyToMajorUnit({ amount: 100, roundAmount: true }) // 'Β£1'

Args​

NameDescriptionTypeDefaultRequired
localeThe locale to be used for formatting. See more in Intlstring"en"false
currencyThe currency to use in currency formatting. Possible values are the ISO 4217 currency codes. See more in Intlstring"GBP"false

Returns​

NameTypeDescriptionExample
formatCurrencyFunctionA currency formatter
formatCurrencyToMajorUnitFunctionConverts and formats a value from a minor unit to a major unit.

formatCurrency​

Converts an amount as a currency string. It supports NumberFormatOptions from Intl.

Import​

import { formatCurrency } from '@krakentech/blueprint-utils';

Usage​

import { formatCurrency } from '@krakentech/blueprint-utils';

formatCurrency({ amount: 1.0, currency: 'EUR', locale: 'fr' }); // '1.00€'
formatCurrency({ amount: 1.0 }); // 'Β£1.00'
formatCurrency({ amount: 5.0, roundAmount: true }); // 'Β£5'
formatCurrency({ amount: '1.12', minimumFractionDigits: 4 }); // 'Β£1.1200'

Args​

NameDescriptionTypeDefaultRequired
amountThe amount to be convertedstring | numbertrue
roundAmountWhether to round the amount to the nearest whole numberstring | numberfalsetrue
localeThe locale to be used for formatting. See more in Intlstring"en"false
currencyThe currency to use in currency formatting. Possible values are the ISO 4217 currency codes. See more in Intlstring"GBP"false
minimumFractionDigitsThe minimum number of fraction digits to use. Takes precedence of roundAmount. See more in Intlnumber0false
maximumFractionDigitsThe maximum number of fraction digits to use. Takes precedence of roundAmount. See more in IntlnumberminimumFractionDigits > 3false

Returns​

TypeDescriptionExample
stringFormatted result"10€"

formatCurrencyToMajorUnit​

Converts an amount to its major unit and formats it as a currency string. It supports NumberFormatOptions from Intl.

Import​

import { formatCurrencyToMajorUnit } from '@krakentech/blueprint-utils';

Usage​

import { formatCurrencyToMajorUnit } from '@krakentech/blueprint-utils';

formatCurrencyToMajorUnit({ amount: 222, currency: 'EUR', locale: 'fr' }); // '2.22€'
formatCurrencyToMajorUnit({ amount: 188 })) // 'Β£1.88'
formatCurrencyToMajorUnit({ amount: 100, roundAmount: true }) // 'Β£1'
formatCurrencyToMajorUnit({ amount: '100', minimumFractionDigits: 4 }); // 'Β£1.0000'

Args​

NameDescriptionTypeDefaultRequired
amountThe amount to be convertedstring | numberfalse
roundAmountWhether to round the amount to the nearest whole numberbooleanfalsefalse
localeThe locale to be used for formatting. See more in Intlstring"en"false
currencyThe currency to use in currency formatting. Possible values are the ISO 4217 currency codes. See more in Intlstring"GBP"false
minimumFractionDigitsThe minimum number of fraction digits to use. Takes precedence of roundAmount. See more in Intlnumber0false
maximumFractionDigitsThe maximum number of fraction digits to use. Takes precedence of roundAmount. See more in IntlnumberminimumFractionDigits > 3false

Returns​

TypeDescriptionExample
stringThe formatted amount"10€"

convertMinorToMajorUnit​

Converts a value from a minor unit to a major unit.

Import​

import { convertMinorToMajorUnit } from '@krakentech/blueprint-utils';

Usage​

import { convertMinorToMajorUnit } from '@krakentech/blueprint-utils';

convertMinorToMajorUnit(25); // 0.25
convertMinorToMajorUnit('2'); // 0.02

Parameters​

DescriptionTypeDefaultRequired
The amount you want to convertstring |Β numbertrue

Returns​

TypeDescriptionExample
numberThe formatted amount0.5

convertMajorToMinorUnit​

Converts a major unit value to a minor unit value.

Import​

import { convertMajorToMinorUnit } from '@krakentech/blueprint-utils';

Usage​

import { convertMajorToMinorUnit } from '@krakentech/blueprint-utils';

convertMajorToMinorUnit(25); // 2500
convertMajorToMinorUnit('2'); // 200

Parameters​

DescriptionTypeDefaultRequired
The amount you want to convertstring |Β numbertrue

Returns​

TypeDescriptionExample
numberThe formatted amount5000

getCurrencySymbol​

Retrieves the currency symbol for a given currency code. It returns the currency symbol based on the provided locale and currency code.

Import​

import { getCurrencySymbol } from '@krakentech/blueprint-utils';

Usage​

import { getCurrencySymbol } from '@krakentech/blueprint-utils';

getCurrencySymbol({ locale: 'fr', currency: 'EUR' }); // €
getCurrencySymbol({ locale: 'en', currency: 'GBP' }); // Β£

Args​

NameDescriptionTypeDefaultRequired
localeThe locale to be used for formatting. See more in Intlstringtrue
currencyThe currency to use in currency formatting. Possible values are the ISO 4217 currency codes. See more in Intlstringtrue

Returns​

TypeDescriptionExample
stringThe currency symbol€

Storyblok​

This subpackage contains Storyblok related utils and types, to ease and mutualise integrations.

Api routes​

Using Storyblok with Next.js needs specific integrations, especially when it comes to story preview and story revalidation. To get fully integrated with Storyblok, your Next.js must provide specific api routes.

createStoryblokApiRoutes builder function​

The createStoryblokApiRoutes exposes required api routes to enable story preview and story revalidation.

Configuration​
OptionTypeRequired
previewSecretstringyes
webhookSecretstringyes
redirectsConfigRedirectsConfigno
Usage​
// /lib/storyblokApiRoutes.ts
import { createStoryblokApiRoutes } from "@krakentech/blueprint-utils/storyblok"

export const storyblokApiRoutes = createStoryblokApiRoutes({
webhookSecret: <your-storyblok-webhook-secret>,
previewSecret: <your-storyblok-preview-secret>,
})
Preview route​

This route is needed to enable preview mode on Storyblok edit view. Read more about previews on Storyblok site

// /pages/api/preview.ts
import { storyblokApiRoutes } from 'lib/storyblokApiRoutes';

export default storyblokApiRoutes.preview;
Exit preview route​

This route is needed to exit preview mode when leaving Storyblok edit view. Read more about previews on Storyblok site

// /pages/api/exit-preview.ts
import { storyblokApiRoutes } from 'lib/storyblokApiRoutes';

export default storyblokApiRoutes.exitPreview;
Revalidate route​

This route enables story revalidation using Storyblok webhooks. To plug the revalidation route to the storyblok hook, make sure that your api route match the one defined in your hook configuration.

NB: only Story triggers are supported.

// /pages/api/revalidate.ts
import { storyblokApiRoutes } from 'lib/storyblokApiRoutes';

export default storyblokApiRoutes.revalidate;

If you are using the next-redirects lib, we suggest you to provide redirectsConfig option to createStoryblokApiRoutes. It will ensure that your build redirections are always synched to your Storyblok ones.

// /lib/storyblokApiRoutes.ts
import { createStoryblokApiRoutes } from "@krakentech/blueprint-utils/storyblok"

export const storyblokApiRoutes = createStoryblokApiRoutes({
webhookSecret: <your-storyblok-webhook-secret>,
previewSecret: <your-storyblok-preview-secret>,
redirectsConfig: {
storySlug: <redirects-config-slug>,
vercelRedeployWebhookUrl: <your-vercel-redeploy-webhook-url>,
}
})

Next Redirects​

Next redirects allows you to generate a list of redirections to be applied in the next config redirects callback.

Prerequisites​

This util needs specific configuration in your Storyblok space in order to work. Please apply the following required steps:

Create a redirect entry nestable block​

Create a nestable block called redirectEntry. Inside this block, create required source and destination fields of type Link.

redirectEntry block in Storyblok

IMPORTANT: Setting the redirect value type to URL comes with specific rules:

  • source entry: the value must be a relative path
  • destination entry: the value can be either a relative path or an absolute path with a custom domain

e.g: redirecting www.<your-domain>.com/hello to www.<your-domain>.com/world using URL values can be done with:

  • source set to "/hello"
  • destination set to "/world"

e.g: redirecting www.<your-domain>.com/hello2 to https://<another-domain>.com/world using URL values can be done with:

  • source set to "/hello2"
  • destination set to "https://<another-domain>.com/world"
Create a redirect config content type block​

Create a content type block called redirectConfig. Inside this block, create a required redirectEntries of type Blocks.

redirectConfig block in Storyblok

Configure the redirectEntries field to allow redirectEntry components only.

redirectEntries field in the redirectConfig block in Storyblok

You should now have both redirectEntry and redirectConfig blocks available in your Storyblok space.

redirectEntry and redirectConfig blocks in Storyblok's block list
Create a redirect config story​

Create a story of type redirectConfig, that will contain your redirects.

redirectConfig story in Storyblok

Edit the story and add your redirectEntry components.

redirectConfig story editor in Storyblok

StoryblokNextRedirects​

OptionTypeRequiredDefault value
accessTokenstringyes
storySlugstringyes
configStoryRedirectOptionsConfigStoryRedirectOptionsno{ destination: '/404', permanent: true }

Usage​

In your next.config.js file, you can use the StoryblokNextRedirects class to generate the redirects.

import { StoryblokNextRedirects } from '@krakentech/blueprint-utils/storyblok';

const storyblokNextRedirects = new StoryblokNextRedirects({
accessToken: "<access-token>",
storySlug: "<redirect-config-story-slug>",
})

const nextConfig = {
...,
async redirects(): {
const storyblokRedirects = await storyblokNextRedirects.generate()
return storyblokRedirects
}
}

Infinite Scroll​

For InfiniteScroll docs see here

Feature Flags​

For the feature flags docs see here