You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

pico-di-remix-express

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pico-di-remix-express

Adapter for using pico-di with Remix + express.

0.1.10
latest
Source
npmnpm
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source
       _                     _ _
 _ __ (_) ___ ___         __| (_)
| '_ \| |/ __/ _ \ _____ / _` | |
| |_) | | (_| (_) |_____| (_| | |
| .__/|_|\___\___/       \__,_|_| + 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

Keywords

di

FAQs

Package last updated on 17 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts