npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flyyer/types

v2.0.3

Published

Flyyer types definition for Typescript templates

Downloads

14

Readme

@flyyer/types

npm-version downloads size tree-shake

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

Install

npm install --save-dev @flyyer/types

yarn add --dev @flyyer/types

Usage

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);
}

Platform information

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.

Import assets

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" />

Experimental Javascript support

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`
  // ...
}

Validate Flyyer Config

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
});