@mastra/deployer-netlify 0.0.0-storage-20250225005900

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,44 @@
1
+ Elastic License 2.0 (ELv2)
2
+
3
+ **Acceptance**
4
+ By using the software, you agree to all of the terms and conditions below.
5
+
6
+ **Copyright License**
7
+ The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below
8
+
9
+ **Limitations**
10
+ You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
11
+
12
+ You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
13
+
14
+ You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
15
+
16
+ **Patents**
17
+ The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
18
+
19
+ **Notices**
20
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
21
+
22
+ If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
23
+
24
+ **No Other Rights**
25
+ These terms do not imply any licenses other than those expressly granted in these terms.
26
+
27
+ **Termination**
28
+ If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
29
+
30
+ **No Liability**
31
+ As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
32
+
33
+ **Definitions**
34
+ The _licensor_ is the entity offering these terms, and the _software_ is the software the licensor makes available under these terms, including any portion of it.
35
+
36
+ _you_ refers to the individual or entity agreeing to these terms.
37
+
38
+ _your company_ is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. _control_ means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
39
+
40
+ _your licenses_ are all the licenses granted to you for the software under these terms.
41
+
42
+ _use_ means anything you do with the software requiring one of your licenses.
43
+
44
+ _trademark_ means trademarks, service marks, and similar rights.
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # @mastra/deployer-netlify
2
+
3
+ A Netlify deployer for Mastra applications.
4
+
5
+ ## Features
6
+
7
+ - Deploy Mastra applications to Netlify Functions
8
+ - Automatic site creation and configuration
9
+ - Serverless function support with Edge Functions
10
+ - Zero-configuration deployments
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ pnpm add @mastra/deployer-netlify
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ The Netlify deployer is used as part of the Mastra framework:
21
+
22
+ ```typescript
23
+ import { Mastra } from '@mastra/core';
24
+ import { NetlifyDeployer } from '@mastra/deployer-netlify';
25
+
26
+ const deployer = new NetlifyDeployer({
27
+ scope: 'your-team-id',
28
+ projectName: 'your-project-name',
29
+ });
30
+
31
+ const mastra = new Mastra({
32
+ deployer,
33
+ // ... other Mastra configuration options
34
+ });
35
+ ```
36
+
37
+ ## Configuration
38
+
39
+ ### Constructor Options
40
+
41
+ - `scope` (required): Your Netlify team ID
42
+ - `projectName`: Name of your Netlify site (will be created if it doesn't exist)
43
+
44
+ ## Project Structure
45
+
46
+ The deployer automatically creates the following structure:
47
+
48
+ ```
49
+ your-project/
50
+ ├── netlify/
51
+ │ └── functions/
52
+ │ └── api/
53
+ └── netlify.toml
54
+ ```
55
+
56
+ ### netlify.toml Configuration
57
+
58
+ The deployer creates a `netlify.toml` with the following defaults:
59
+
60
+ ```toml
61
+ [functions]
62
+ node_bundler = "esbuild"
63
+ directory = "/netlify/functions"
64
+
65
+ [[redirects]]
66
+ force = true
67
+ from = "/*"
68
+ status = 200
69
+ to = "/.netlify/functions/api/:splat"
70
+ ```
71
+
72
+ ## Environment Variables
73
+
74
+ Environment variables are handled automatically through:
75
+
76
+ - `.env` files in your project
77
+ - Environment variables passed through the Mastra configuration
78
+ - Netlify's environment variable UI
79
+
80
+ ## Deployment Process
81
+
82
+ The deployer will:
83
+
84
+ 1. Create a new site if it doesn't exist
85
+ 2. Configure the site with your environment variables
86
+ 3. Deploy your application to Netlify Functions
@@ -0,0 +1,38 @@
1
+ import { Deployer } from '@mastra/deployer';
2
+
3
+ export declare function getOrCreateSite({ token, name, scope }: {
4
+ token: string;
5
+ name: string;
6
+ scope: string;
7
+ }): Promise<{
8
+ id: string | undefined;
9
+ name: string | undefined;
10
+ url: string | undefined;
11
+ adminUrl: string | undefined;
12
+ } | {
13
+ id: string;
14
+ name: string;
15
+ ssl_url: string;
16
+ url: string;
17
+ admin_url: string;
18
+ }>;
19
+
20
+ export declare class NetlifyDeployer extends Deployer {
21
+ protected scope: string;
22
+ protected projectName: string;
23
+ protected token: string;
24
+ constructor({ scope, projectName, token }: {
25
+ scope: string;
26
+ projectName: string;
27
+ token: string;
28
+ });
29
+ writeFiles({ dir }: {
30
+ dir: string;
31
+ }): void;
32
+ deploy(outputDirectory: string): Promise<void>;
33
+ prepare(outputDirectory: string): Promise<void>;
34
+ bundle(entryFile: string, outputDirectory: string): Promise<void>;
35
+ private getEntry;
36
+ }
37
+
38
+ export { }
@@ -0,0 +1 @@
1
+ export { NetlifyDeployer } from './_tsup-dts-rollup.js';
package/dist/index.js ADDED
@@ -0,0 +1,138 @@
1
+ import { Deployer } from '@mastra/deployer';
2
+ import { execa } from 'execa';
3
+ import { existsSync, mkdirSync, writeFileSync } from 'fs';
4
+ import { join } from 'path';
5
+
6
+ // src/index.ts
7
+
8
+ // src/helpers.ts
9
+ async function createNetlifySite({ token, name, scope }) {
10
+ const response = await fetch("https://api.netlify.com/api/v1/sites", {
11
+ method: "POST",
12
+ headers: {
13
+ Authorization: `Bearer ${token}`,
14
+ "Content-Type": "application/json"
15
+ },
16
+ body: JSON.stringify({
17
+ name,
18
+ account_slug: scope,
19
+ // Optional - if not provided, creates in user's default account
20
+ force_ssl: true
21
+ // Enable HTTPS
22
+ })
23
+ });
24
+ const data = await response.json();
25
+ if (!response.ok) {
26
+ console.error(JSON.stringify(data));
27
+ throw new Error(`Failed to create site: ${data.message || "Unknown error"}`);
28
+ }
29
+ return {
30
+ id: data.id,
31
+ name: data.name,
32
+ url: data.ssl_url || data.url,
33
+ adminUrl: data.admin_url
34
+ };
35
+ }
36
+ async function findNetlifySite({ token, name, scope }) {
37
+ const response = await fetch(`https://api.netlify.com/api/v1/${scope}/sites?filter=all&name=${name}`, {
38
+ headers: {
39
+ Authorization: `Bearer ${token}`,
40
+ "Content-Type": "application/json"
41
+ }
42
+ });
43
+ const data = await response.json();
44
+ if (!response.ok || !Array.isArray(data)) {
45
+ throw new Error(`Failed to search sites: ${data.message || "Unknown error"}`);
46
+ }
47
+ return data.find((site) => site.name === name);
48
+ }
49
+ async function getOrCreateSite({ token, name, scope }) {
50
+ let existingSite;
51
+ try {
52
+ existingSite = await findNetlifySite({ token, name, scope });
53
+ } catch (e) {
54
+ }
55
+ if (existingSite) {
56
+ return existingSite;
57
+ }
58
+ return createNetlifySite({ token, name, scope });
59
+ }
60
+
61
+ // src/index.ts
62
+ var NetlifyDeployer = class extends Deployer {
63
+ scope;
64
+ projectName;
65
+ token;
66
+ constructor({ scope, projectName, token }) {
67
+ super({ name: "NETLIFY" });
68
+ this.scope = scope;
69
+ this.projectName = projectName;
70
+ this.token = token;
71
+ }
72
+ writeFiles({ dir }) {
73
+ if (!existsSync(join(dir, "netlify/functions/api"))) {
74
+ mkdirSync(join(dir, "netlify/functions/api"), { recursive: true });
75
+ }
76
+ writeFileSync(
77
+ join(dir, "netlify.toml"),
78
+ `[functions]
79
+ node_bundler = "esbuild"
80
+ directory = "netlify/functions"
81
+
82
+ [[redirects]]
83
+ force = true
84
+ from = "/*"
85
+ status = 200
86
+ to = "/.netlify/functions/api/:splat"
87
+ `
88
+ );
89
+ }
90
+ async deploy(outputDirectory) {
91
+ const site = await getOrCreateSite({ token: this.token, name: this.projectName || `mastra`, scope: this.scope });
92
+ const p2 = execa(
93
+ "npx",
94
+ [
95
+ "netlify-cli",
96
+ "deploy",
97
+ "--site",
98
+ site.id,
99
+ "--auth",
100
+ this.token,
101
+ "--dir",
102
+ ".",
103
+ "--functions",
104
+ "./netlify/functions"
105
+ ],
106
+ {
107
+ cwd: join(outputDirectory, this.outputDir)
108
+ }
109
+ );
110
+ p2.stdout.pipe(process.stdout);
111
+ await p2;
112
+ }
113
+ async prepare(outputDirectory) {
114
+ await super.prepare(outputDirectory);
115
+ this.writeFiles({ dir: join(outputDirectory, this.outputDir) });
116
+ }
117
+ async bundle(entryFile, outputDirectory) {
118
+ return this._bundle(
119
+ this.getEntry(),
120
+ entryFile,
121
+ outputDirectory,
122
+ join(outputDirectory, this.outputDir, "netlify", "functions", "api")
123
+ );
124
+ }
125
+ getEntry() {
126
+ return `
127
+ import { handle } from 'hono/netlify'
128
+ import { mastra } from '#mastra';
129
+ import { createHonoServer } from '#server';
130
+
131
+ const app = await createHonoServer(mastra);
132
+
133
+ export default handle(app)
134
+ `;
135
+ }
136
+ };
137
+
138
+ export { NetlifyDeployer };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@mastra/deployer-netlify",
3
+ "version": "0.0.0-storage-20250225005900",
4
+ "description": "",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "keywords": [],
16
+ "author": "",
17
+ "license": "ISC",
18
+ "dependencies": {
19
+ "@rollup/plugin-virtual": "^3.0.2",
20
+ "date-fns": "^4.1.0",
21
+ "execa": "^9.3.1",
22
+ "netlify-cli": "^18.0.1",
23
+ "zod": "^3.24.1",
24
+ "@mastra/core": "^0.0.0-storage-20250225005900",
25
+ "@mastra/deployer": "^0.0.0-storage-20250225005900"
26
+ },
27
+ "devDependencies": {
28
+ "@microsoft/api-extractor": "^7.49.2",
29
+ "@types/node": "^22.13.1",
30
+ "tsup": "^8.0.1",
31
+ "typescript": "^5.7.3",
32
+ "vitest": "^3.0.4"
33
+ },
34
+ "scripts": {
35
+ "build": "tsup src/index.ts --format esm --experimental-dts --clean --treeshake",
36
+ "build:watch": "pnpm build --watch",
37
+ "test": "vitest run"
38
+ }
39
+ }
package/src/helpers.ts ADDED
@@ -0,0 +1,76 @@
1
+ async function createNetlifySite({ token, name, scope }: { token: string; name: string; scope?: string }) {
2
+ const response = await fetch('https://api.netlify.com/api/v1/sites', {
3
+ method: 'POST',
4
+ headers: {
5
+ Authorization: `Bearer ${token}`,
6
+ 'Content-Type': 'application/json',
7
+ },
8
+ body: JSON.stringify({
9
+ name: name,
10
+ account_slug: scope, // Optional - if not provided, creates in user's default account
11
+ force_ssl: true, // Enable HTTPS
12
+ }),
13
+ });
14
+
15
+ const data = (await response.json()) as
16
+ | {
17
+ id: string;
18
+ name: string;
19
+ ssl_url: string;
20
+ url: string;
21
+ admin_url: string;
22
+ message?: never;
23
+ }
24
+ | { message: string; id?: never; name?: never; ssl_url?: never; url?: never; admin_url?: never };
25
+
26
+ if (!response.ok) {
27
+ console.error(JSON.stringify(data));
28
+ throw new Error(`Failed to create site: ${data.message || 'Unknown error'}`);
29
+ }
30
+
31
+ return {
32
+ id: data.id,
33
+ name: data.name,
34
+ url: data.ssl_url || data.url,
35
+ adminUrl: data.admin_url,
36
+ };
37
+ }
38
+
39
+ async function findNetlifySite({ token, name, scope }: { token: string; name: string; scope: string }) {
40
+ const response = await fetch(`https://api.netlify.com/api/v1/${scope}/sites?filter=all&name=${name}`, {
41
+ headers: {
42
+ Authorization: `Bearer ${token}`,
43
+ 'Content-Type': 'application/json',
44
+ },
45
+ });
46
+
47
+ const data = (await response.json()) as
48
+ | {
49
+ id: string;
50
+ name: string;
51
+ ssl_url: string;
52
+ url: string;
53
+ admin_url: string;
54
+ }[]
55
+ | { message: string };
56
+
57
+ if (!response.ok || !Array.isArray(data)) {
58
+ throw new Error(`Failed to search sites: ${(data as { message: string }).message || 'Unknown error'}`);
59
+ }
60
+
61
+ // Find exact match (filter can return partial matches)
62
+ return data.find(site => site.name === name);
63
+ }
64
+
65
+ export async function getOrCreateSite({ token, name, scope }: { token: string; name: string; scope: string }) {
66
+ let existingSite;
67
+ try {
68
+ existingSite = await findNetlifySite({ token, name, scope });
69
+ } catch (e) {}
70
+
71
+ if (existingSite) {
72
+ return existingSite;
73
+ }
74
+
75
+ return createNetlifySite({ token, name, scope });
76
+ }
package/src/index.ts ADDED
@@ -0,0 +1,95 @@
1
+ import { Deployer } from '@mastra/deployer';
2
+ import { execa } from 'execa';
3
+ import { existsSync, mkdirSync, writeFileSync } from 'fs';
4
+ import { join } from 'path';
5
+
6
+ import { getOrCreateSite } from './helpers.js';
7
+
8
+ export class NetlifyDeployer extends Deployer {
9
+ protected scope: string;
10
+ protected projectName: string;
11
+ protected token: string;
12
+
13
+ constructor({ scope, projectName, token }: { scope: string; projectName: string; token: string }) {
14
+ super({ name: 'NETLIFY' });
15
+
16
+ this.scope = scope;
17
+ this.projectName = projectName;
18
+ this.token = token;
19
+ }
20
+
21
+ writeFiles({ dir }: { dir: string }): void {
22
+ if (!existsSync(join(dir, 'netlify/functions/api'))) {
23
+ mkdirSync(join(dir, 'netlify/functions/api'), { recursive: true });
24
+ }
25
+
26
+ // TODO ENV KEYS
27
+ writeFileSync(
28
+ join(dir, 'netlify.toml'),
29
+ `[functions]
30
+ node_bundler = "esbuild"
31
+ directory = "netlify/functions"
32
+
33
+ [[redirects]]
34
+ force = true
35
+ from = "/*"
36
+ status = 200
37
+ to = "/.netlify/functions/api/:splat"
38
+ `,
39
+ );
40
+ }
41
+
42
+ async deploy(outputDirectory: string): Promise<void> {
43
+ const site = await getOrCreateSite({ token: this.token, name: this.projectName || `mastra`, scope: this.scope });
44
+
45
+ // @ts-expect-error - seems to be fine
46
+ const p2 = execa(
47
+ 'npx',
48
+ [
49
+ 'netlify-cli',
50
+ 'deploy',
51
+ '--site',
52
+ site.id,
53
+ '--auth',
54
+ this.token,
55
+ '--dir',
56
+ '.',
57
+ '--functions',
58
+ './netlify/functions',
59
+ ],
60
+ {
61
+ cwd: join(outputDirectory, this.outputDir),
62
+ },
63
+ );
64
+
65
+ p2.stdout.pipe(process.stdout);
66
+ await p2;
67
+ }
68
+
69
+ async prepare(outputDirectory: string): Promise<void> {
70
+ await super.prepare(outputDirectory);
71
+
72
+ this.writeFiles({ dir: join(outputDirectory, this.outputDir) });
73
+ }
74
+
75
+ async bundle(entryFile: string, outputDirectory: string): Promise<void> {
76
+ return this._bundle(
77
+ this.getEntry(),
78
+ entryFile,
79
+ outputDirectory,
80
+ join(outputDirectory, this.outputDir, 'netlify', 'functions', 'api'),
81
+ );
82
+ }
83
+
84
+ private getEntry(): string {
85
+ return `
86
+ import { handle } from 'hono/netlify'
87
+ import { mastra } from '#mastra';
88
+ import { createHonoServer } from '#server';
89
+
90
+ const app = await createHonoServer(mastra);
91
+
92
+ export default handle(app)
93
+ `;
94
+ }
95
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.node.json",
3
+ "include": ["src/**/*"],
4
+ "exclude": ["node_modules", "**/*.test.ts"]
5
+ }
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: 'node',
6
+ include: ['src/**/*.test.ts'],
7
+ },
8
+ });