Giter Site home page Giter Site logo

near-prpsls-bos's Introduction

TypeScript Starter Project for NEAR BOS

NEAR BOS (Blockchain OS) is an excellent foundation for decentralized front-end apps. Since BOS expects JSX to run, I always wanted to bring it to another level with TypeScript support.

Here is a TSX BOS component (you can also find it in src/components/pages/homepage.tsx):

// Welcome to the home page of the first TypeScript BOS component!

// TypeScript! Yay!
interface Props {
  customWelcomeMessage?: string;
}

// Just create a default export function (no need to `return` it, see `.bos`
// folder after `npm run build` if you want to understand what is happening)
export default function (props: Props, context: BosContext) {
  return (
    <>
      <h1>
        {props.customWelcomeMessage ??
          "Welcome to the home page of the first TypeScript BOS component"}
        , {context.accountId ?? "anonymous user"}
      </h1>
      <p>
        Learn more at{" "}
        <a href="https://github.com/frol/bos-component-ts-starter">
          BOS Component TypeScript Starter repo
        </a>
      </p>
      <Widget
        src="frol.near/widget/bos-component-ts-starter.components.subfolder.my-nested-component"
        props={{ color: "green" }}
      />
    </>
  );
}

This is a preconfigured project that puts things together:

You can also find several auxiliary files in this repo:

  • build.js handles several useful features:
    • automatically returns the export default function as BOS component, so you don't need to have a free-standing return <MyComponent props={props} /> statement at the end of your file.
    • mimics standard import ... from ... syntax for files saved in src/includes/ folder (see how to use imports here)
    • automatically adds license, author, and homepage link from package.json to the headers of each BOS component
  • tsconfig.json is used by VS Code to properly resolve types and project structure
  • global.d.ts is used to inject types of <Widget> and BosContext, and ignore non-existing React dependency.

Putting all those pieces together, a fully working starter project in TypeScript was born. If you develop in VS Code, it should properly highlight issues with types now, and allow you to define your own types to ensure consistency of your code-base. Please, report any problems with VS Code or your editor of choice and contribute fixed by proposing pull requests.

How to run locally

Learn about BOS-LOADER more

  1. Change devgovgigs.near to your account name in package.json "dev": "~/.cargo/bin/bos-loader devgovgigs.near --path ./.bos/transpiled/src",
  2. Open https://near.org/flags, and set the loader URL to http://127.0.0.1:3030
  3. Run yarn build
  4. Then run yarn dev
  5. Open https://near.org/<youraccount.near>/widget/<component name> (case sensitive) For example https://near.org/devgovgigs.near/widget/bos-component-ts-starter.components.pages.homepage
  6. Make changes to the component's code. Repeate steps 2-5 to see the changes.

How to Use

Learn about BOS, and consider building your first components without this starter project as it will be easier to get started with an in-browser playground. Once you are ready to build a complex app on BOS using TypeScript:

  1. Fork this project
  2. Install dependencies: npm install
  3. Edit components
  4. Deploy: npm run deploy - bos CLI will interactively ask for the details like which account you want to deploy the components to and how to sign the transaction

Troubleshooting

npm run build command will create .bos folder in the root of the project, and you can inspect the generated JSX code there.

License

This repository is distributed under the terms of the MIT license. See LICENSE-MIT for details.

near-prpsls-bos's People

Contributors

megha-dev-19 avatar petersalomonsen avatar frol avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.