https://github.com/jorgenvatle/meteor-vite-plugin-type-api
A Vite plugin for defining type-safe Meteor methods and publications
https://github.com/jorgenvatle/meteor-vite-plugin-type-api
meteor meteor-vite typescript valibot vite zodern-relay
Last synced: 5 months ago
JSON representation
A Vite plugin for defining type-safe Meteor methods and publications
- Host: GitHub
- URL: https://github.com/jorgenvatle/meteor-vite-plugin-type-api
- Owner: JorgenVatle
- License: mit
- Created: 2025-08-14T00:58:40.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2025-08-16T17:54:16.000Z (5 months ago)
- Last Synced: 2025-08-16T19:24:00.797Z (5 months ago)
- Topics: meteor, meteor-vite, typescript, valibot, vite, zodern-relay
- Language: TypeScript
- Homepage:
- Size: 245 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @meteor-vite/type-api
[](https://npmjs.com/package/@meteor-vite/type-api)
[](https://github.com/JorgenVatle/meteor-vite-plugin-type-api/actions/workflows/test.yml)
[](https://github.com/JorgenVatle/meteor-vite-plugin-type-api/actions/workflows/lint.yml)
[](https://github.com/JorgenVatle/meteor-vite-plugin-type-api/actions/workflows/release.yml)
A Vite plugin for generating type-safe Meteor methods and publications.
Follows the same pattern as [`zodern:relay`](https://github.com/zodern/meteor-relay) with some minor differences:
- [Valibot](https://valibot.dev/) is used for schema validation as opposed to [`zod`](https://zod.dev/)
- Relay modules are parsed with Rollup's built-in AST parser.
- Method and publication directories are configurable through Vite allowing for methods to be defined by either a custom file extension, directory name or both.
- No support for pipelines or optional custom method stubs.
> [!NOTE]
> If you want to use [`zodern:relay`](https://github.com/zodern/meteor-relay) with Vite, instead use [`@meteor-vite/plugin-zodern-relay`](https://github.com/JorgenVatle/meteor-vite/tree/release/npm-packages/%40meteor-vite/plugin-zodern-relay).
## Installation
Add the package to your project:
```bash
npm install @meteor-vite/type-api
```
## Configuration
In your `vite.config.ts` file:
```typescript
import meteorApiTypes from '@meteor-vite/type-api/plugin';
export default defineConfig({
plugins: [
meteorApiTypes({
// ...
}),
]
})
```
View all configuration options
```typescript
meteorApiTypes({
/**
* Treats the provided directory names as Meteor methods/publications.
* Publications and methods can share the same directory or file extension
* if you want to manage both in the same file.
* @optional
*/
dirname: {
/**
* Parent directory for Meteor methods.
*
* All files within directories matching this name will be treated as
* Meteor methods regardless of their file extension.
*
* @default methods
*/
methods: string;
/**
* Parent directory for Meteor publications.
*
* All files within directories matching this name will be treated as
* Meteor publications regardless of their file extension.
*
* @default publications
*/
publications: string;
},
/**
* Treats the provided file extensions as Meteor methods/publications.
* @optional
*/
fileExtension: {
/**
* File extension for Meteor methods.
*
* This can be used as an alternative to nesting methods under a
* methods directory.
*
* @default .methods.ts
*/
methods: string;
/**
* File extension for Meteor publications.
*
* This can be used as an alternative to nesting publications under a
* publications directory.
*
* @default .publications.ts
*/
publications: string;
},
})
```
## Methods
### Defining methods
Define your Meteor methods in a file with a `.methods.ts` extension or nest them under a `methods/` parent directory.
Example filename: `/imports/api/links.methods.ts`
```typescript
import { defineMethod } from '@meteor-vite/type-api';
export const createLink = defineMethod({
schema: v.object({
href: v.string(),
description: v.optional(v.string()),
}),
run({ description, href }) {
return Links.insert({ description, href });
}
});
```
### Calling methods
Methods can be imported directly from anywhere in your app. This works both on the server and client.
```typescript
import { createLink } from '/imports/api/links.methods';
createLink({
href: 'https://example.com',
description: 'Example link'
}).then((id) => {
console.log(`Created link: ${id}`);
})
```
## Publications
### Defining publications
Define your Meteor publications in a file with a `.publications.ts` extension or nest them under a `publications/` parent directory.
Example filename: `/imports/api/links.publications.ts`
```typescript
import { definePublication } from '@meteor-vite/type-api';
export const getLinks = definePublication({
schema: v.object({
createdAt: v.object({
$gt: v.optional(v.date()),
$lt: v.optional(v.date()),
})
}),
run(query) {
return Links.find(query);
}
});
```
### Subscribing to publications
Publications can be imported directly from anywhere in your app. This works both on the server and client.
```typescript
import { getLinks } from '/imports/api/links.publications';
const subscription = getLinks({
createdAt: {
$lt: new Date(2025, 0, 1),
}
});
Tracker.autorun(() => {
console.log({
ready: subscription.ready(),
});
})
```
## Alternative packages
- [`zodern:relay`](https://github.com/zodern/meteor-relay) - Meteor build plugin for type safe Meteor publications and methods.
- [`meteor-type-validation`](https://github.com/JorgenVatle/meteor-type-validation) - Type-safe Meteor methods and publications done entirely through TypeScript types. Requires no build or compilation steps.
- [`@meteor-vite/plugin-zodern-relay`](https://github.com/JorgenVatle/meteor-vite/tree/release/npm-packages/%40meteor-vite/plugin-zodern-relay) - Vite compatability plugin for [`zodern:relay`](https://github.com/zodern/meteor-relay). (For apps that use [`meteor-vite`](https://github.com/JorgenVatle/meteor-vite))
## License
This repository is licensed under the MIT license.
Copyright (c) 2025, Jørgen Vatle