JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q55162F
  • License MIT

Adapter for using pico-di with Remix + express.

Package Exports

  • pico-di-remix-express
  • pico-di-remix-express/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pico-di-remix-express) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

       _                     _ _
 _ __ (_) ___ ___         __| (_)
| '_ \| |/ __/ _ \ _____ / _` | |
| |_) | | (_| (_) |_____| (_| | |
| .__/|_|\___\___/       \__,_|_| + Remix + express
|_|

Adapter for using pico-di with Remix + express.

npm version Socket


pico-di を Remix + express 環境で使うためのアダプタです。

Features

pico-di-remix-express には pico-di に加えて以下の機能があります:

  • action / loader インジェクション
  • リクエスト毎のスコープ
  • シングルトンサービスの対応
    • ライブリロードできます
    • (開発ビルドにおいて) リクエスト間であってもシングルトンサービスがシングルトンであることを保証します

Installation

Remix + express (@remix-run/express) に対応しています。

Node.js

npm から入手できます。インストールするには:

$ npm install pico-di-remix-express

Usage

サービスは全て ~/services ディレクトリ以下にまとめ、~/services/index.ts でそれぞれインポートします。プロバイダ作成関数をエクスポートする必要があります。

// /app/services/index.ts

import { createGetProvider } from "pico-di-remix-express";
import Service1 from "./Service1";
import Service2 from "./Service2";
import Service3 from "./Service3";

type Context = {
    service1: Service1,
    service2: Service2,
    service3: Service3,
};

// `getProvider` という名前にしておきます。
const getProvider = createGetProvider<Context>(builder => builder
    .addSingleton("service1", Service1)
    .addScoped("service2", Service2)
    .addTransient("service3", Service3));

export {
    getProvider,
    type Context,
    type Service1,
    type Service2,
    type Service3,
}

/app/entry.server.tsx の修正

先ほどの getProvider という関数を ~/entry.server.tsx からエクスポートする必要があります。このエクスポートは pico-di-remix-express がサービスプロバイダを取得するために必要です。

// /app/entry.server.tsx

export { getProvider } from "~/services";

// ...

/server.js の修正

action / loader でサービスを使う

import { type LoaderArgs, type ActionArgs } from "pico-di-remix-express";
import { type Context } from "~/services";

export const loader = ({
    context: {
        service1,
        service2,
        service3,
    },
}: LoaderArgs<Context>) => {
    // ...
};

export const action = ({
    context: {
        service1,
        service2,
        service3,
    },
}: ActionArgs<Context>) => {
    // ...
};

Reference

License

MIT