Flyyer type definition for Typescript templates created with create-flyyer-app.
๐ Want to learn more about rendering images? Visit us at flyyer.io
๐ Checkout our official documentation: docs.flyyer.io
npm install --save-dev @flyyer/types
yarn add --dev @flyyer/types
The provided TemplateProps<T>
accepts a generic object type for the possible variables passed to the template. Keep in mind not every variable is guaranteed to be present, this is the reason why every variable will be marked as possibly undefined.
import React from "react";
import { TemplateProps } from "@flyyer/types";
export default function SimpleTemplate({ variables }: TemplateProps) {
const title = variables.title; // type is `string | undefined`;
return (
<div>
{title && <h1>{title}</h1>}
</div>
);
}
Since URL serialization converts Date
and number
to strings, every field type will be typed as string | undefined
. Objects and arrays will be also typed using a recursive strategy.
import React from "react";
import { TemplateProps } from "@flyyer/types";
// Example:
type Variables = {
title: string;
count: number;
price: number;
created: Date;
object: {
name: string;
age?: number;
};
array?: number[];
}
export default function Template({ variables }: TemplateProps<Variables>) {
const {
title, // type is `string | undefined`
count, // type is `string | undefined`
price = 10, // type is `string | 10` because incoming values will be string!
createdAt, // type is `string | undefined`
object, // type is a recursive object with `string | undefined` values: `{ name?: string | age?: string } | undefined`
array, // type is a recursive array with `string | undefined` values: `(string | undefined)[] | undefined`
} = variables;
return (
<div>
{title && <h1>{title}</h1>}
</div>
);
}
To avoid this issue we encourage using @flyyer/variables which can parse and coerce types and handle default values.
import { Variable as V, Static } from "@flyyer/variables";
// Example:
export const schema = V.Object({
title: V.String({ description: "Show this on https://flyyer.io" }),
count: V.Integer({ title: "Count of items" }),
price: V.Number({ default: 10.0 }),
createdAt: V.Optional(V.String({ format: "date-time" })),
object: V.Object({
name: V.String(),
age: V.Integer(),
}),
array: V.Array(V.Number(), { description: "An array of numbers" }),
});
type Variables = Static<typeof schema>;
export default function Template({ variables }: TemplateProps<Variables>) {
const {
data: {
title, // type is `string`,
count, // type is `number`
price, // type is `number`
createdAt, // type is `string | undefined` and a valid for `new Date(...)`
object, // type if the expected object type
array, // type is `number[]`
},
} = validator.parse(variables);
}
Sometimes we can identify which platform your link are being shared on. You can get this information via the agent
prop.
import React from "react";
import { TemplateProps, FlyyerAgentName } from "@flyyer/types";
export default function MainTemplate({ agent }: TemplateProps) {
if (agent.name === FlyyerAgentName.WHATSAPP) {
// Custom rules for squared template
return ...
} else {
// Default 1200x630 banner.
return ...
}
}
Handle multiple locales (languages) by reading the locale
prop. You can use some lightweight libraries to process locale internationalization (i18n) just like: lukeed/rosetta and airbnb/polyglot.js.
export default function MainTemplate({ locale }: TemplateProps) {
if (!locale) {
// no locale info was provided
} else if (locale === "en") {
// ...
} else if (locale === "es-CO") {
// ...
}
// Native Intl module is supported โ
const formatter = new Intl.DateTimeFormat(locale);
const humanized = formatter.format(new Date());
}
NOTE: Sometimes web proxies used by crawlers ignores user's actual locale.
To remove Typescript warning when importing files such as images, fonts, style files, etc.
Use the following code in a types.d.ts
file in the root of your Flyyer deck.
// types.d.ts
/// <reference types="@flyyer/types/global" />
You can help your IDE with typing information. Here is an working but experimental example in Visual Studio Code:
import { TemplateProps } from "@flyyer/types"; // eslint-disable-line no-unused-vars
/**
* Make sure to default export a React component
* @param {TemplateProps} [props] - Flyyer props.
*/
export default function JavascriptTemplate({ variables }) {
const title = variables.title; // IDE will suggest `title` has type `string | undefined`
// ...
}
This is optional but will help your IDE with IntelliSense to autocomplete and hint you.
// flyyer.config.js
const { config } = require("@flyyer/types");
require("dotenv").config();
module.exports = config({
engine: "react",
key: process.env.FLYYER_KEY,
deck: "my-deck",
// Optionals
name: "My Deck",
description: "Lorem ipsum with **markdown**",
private: false, // Make deck public on https://flyyer.io/community when `false`.
license: "MIT",
keywords: ["keyword"],
sizes: ["THUMBNAIL", "BANNER", "SQUARE", "STORY"], // supported sizes
repository: "https://github.com/useflyyer/repository", // show source on https://flyyer.io/community
});