@adonisjs/content 1.1.0
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.md +9 -0
- package/README.md +290 -0
- package/build/chunk-LB6JFRVG.js +7 -0
- package/build/chunk-ZX3KSI7U.js +130 -0
- package/build/configure.d.ts +5 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +16 -0
- package/build/providers/content_provider.d.ts +46 -0
- package/build/providers/content_provider.js +45 -0
- package/build/src/collection.d.ts +112 -0
- package/build/src/debug.d.ts +11 -0
- package/build/src/loaders/gh_contributors.d.ts +49 -0
- package/build/src/loaders/gh_releases.d.ts +52 -0
- package/build/src/loaders/gh_sponsors.d.ts +56 -0
- package/build/src/loaders/json.d.ts +43 -0
- package/build/src/loaders/main.d.ts +91 -0
- package/build/src/loaders/main.js +648 -0
- package/build/src/types.d.ts +280 -0
- package/build/src/utils.d.ts +82 -0
- package/package.json +146 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
# @adonisjs/content
|
|
2
|
+
|
|
3
|
+
<br />
|
|
4
|
+
|
|
5
|
+
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
|
|
6
|
+
|
|
7
|
+
## Introduction
|
|
8
|
+
|
|
9
|
+
A type-safe content management package for AdonisJS that enables you to create validated collections with schema validation and use loaders to load data.
|
|
10
|
+
|
|
11
|
+
The main goal of this package is to use load data from JSON files for creating documentation websites or blog.
|
|
12
|
+
|
|
13
|
+
We use this package for all official AdonisJS websites to manage docs, blog posts, Github sponsors data, Github releases, and so on.
|
|
14
|
+
|
|
15
|
+
**Key Features:**
|
|
16
|
+
|
|
17
|
+
- **Type-safe collections** with VineJS schema validation
|
|
18
|
+
- **GitHub loaders** for sponsors, releases, and contributors
|
|
19
|
+
- **Custom query methods** for filtering and transforming data
|
|
20
|
+
- **JSON file loading** with validation
|
|
21
|
+
- **Vite integration** for asset path resolution
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Install the package from npm registry as follows:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm i @adonisjs/content
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
yarn add @adonisjs/content
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
pnpm add @adonisjs/content
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Configuration
|
|
40
|
+
|
|
41
|
+
The package requires `@adonisjs/core`, `@adonisjs/vite`, and `@vinejs/vine` as peer dependencies. Run the following command to register the `@adonisjs/content/content_provider` to the `adonisrc.ts` file.
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
node ace configure @adonisjs/content
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
### Creating a Collection
|
|
50
|
+
|
|
51
|
+
Collections are the core building blocks for managing typed content. Create a collection by providing a VineJS schema and a loader:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import vine from '@vinejs/vine'
|
|
55
|
+
import app from '@adonisjs/core/services/app'
|
|
56
|
+
import { Collection } from '@adonisjs/content'
|
|
57
|
+
import { loaders } from '@adonisjs/content/loaders'
|
|
58
|
+
|
|
59
|
+
// Define a schema
|
|
60
|
+
const postSchema = vine.object({
|
|
61
|
+
title: vine.string(),
|
|
62
|
+
slug: vine.string(),
|
|
63
|
+
content: vine.string(),
|
|
64
|
+
published: vine.boolean(),
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
// Create a collection with custom views
|
|
68
|
+
const posts = new Collection({
|
|
69
|
+
schema: vine.array(postSchema),
|
|
70
|
+
loader: loaders.jsonLoader(app.makePath('data/posts.json')),
|
|
71
|
+
cache: true,
|
|
72
|
+
views: {
|
|
73
|
+
published: (posts) => posts.filter((p) => p.published),
|
|
74
|
+
findBySlug: (posts, slug: string) => posts.find((p) => p.slug === slug),
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
// Query the collection
|
|
79
|
+
const query = await posts.load()
|
|
80
|
+
const allPosts = query.all()
|
|
81
|
+
const publishedPosts = query.published()
|
|
82
|
+
const post = query.findBySlug('hello-world')
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### GitHub Sponsors Loader
|
|
86
|
+
|
|
87
|
+
Fetch and cache GitHub sponsors data:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import vine from '@vinejs/vine'
|
|
91
|
+
import app from '@adonisjs/core/services/app'
|
|
92
|
+
import { Collection } from '@adonisjs/content'
|
|
93
|
+
import { loaders } from '@adonisjs/content/loaders'
|
|
94
|
+
|
|
95
|
+
const sponsorSchema = vine.object({
|
|
96
|
+
id: vine.string(),
|
|
97
|
+
sponsorLogin: vine.string(),
|
|
98
|
+
sponsorName: vine.string().nullable().optional(),
|
|
99
|
+
tierMonthlyPriceInCents: vine.number().nullable().optional(),
|
|
100
|
+
// ... other fields
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
const sponsorsLoader = loaders.ghSponsors({
|
|
104
|
+
login: 'adonisjs',
|
|
105
|
+
isOrg: true,
|
|
106
|
+
ghToken: process.env.GITHUB_TOKEN!,
|
|
107
|
+
outputPath: app.makePath('cache/sponsors.json'),
|
|
108
|
+
refresh: 'daily',
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const sponsors = new Collection({
|
|
112
|
+
schema: vine.array(sponsorSchema),
|
|
113
|
+
loader: sponsorsLoader,
|
|
114
|
+
cache: true,
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
const query = await sponsors.load()
|
|
118
|
+
const allSponsors = query.all()
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### GitHub Releases Loader
|
|
122
|
+
|
|
123
|
+
Fetch releases from all public repositories in an organization:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import vine from '@vinejs/vine'
|
|
127
|
+
import app from '@adonisjs/core/services/app'
|
|
128
|
+
import { Collection } from '@adonisjs/content'
|
|
129
|
+
import { loaders } from '@adonisjs/content/loaders'
|
|
130
|
+
|
|
131
|
+
const releasesLoader = loaders.ghReleases({
|
|
132
|
+
org: 'adonisjs',
|
|
133
|
+
ghToken: process.env.GITHUB_TOKEN!,
|
|
134
|
+
outputPath: app.makePath('cache/releases.json'),
|
|
135
|
+
refresh: 'daily',
|
|
136
|
+
filters: {
|
|
137
|
+
nameDoesntInclude: ['alpha', 'beta', 'rc'],
|
|
138
|
+
nameIncludes: ['adonis'],
|
|
139
|
+
},
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
const releases = new Collection({
|
|
143
|
+
schema: vine.array(releaseSchema),
|
|
144
|
+
loader: releasesLoader,
|
|
145
|
+
cache: true,
|
|
146
|
+
views: {
|
|
147
|
+
latest: (releases) => releases.slice(0, 5),
|
|
148
|
+
byRepo: (releases, repo: string) => releases.filter((r) => r.repo === repo),
|
|
149
|
+
},
|
|
150
|
+
})
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### GitHub Contributors Loader
|
|
154
|
+
|
|
155
|
+
Aggregate contributors from all public repositories:
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import vine from '@vinejs/vine'
|
|
159
|
+
import app from '@adonisjs/core/services/app'
|
|
160
|
+
import { Collection } from '@adonisjs/content'
|
|
161
|
+
import { loaders } from '@adonisjs/content/loaders'
|
|
162
|
+
|
|
163
|
+
const contributorsLoader = loaders.ghContributors({
|
|
164
|
+
org: 'adonisjs',
|
|
165
|
+
ghToken: process.env.GITHUB_TOKEN!,
|
|
166
|
+
outputPath: app.makePath('cache/contributors.json'),
|
|
167
|
+
refresh: 'weekly',
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
const contributors = new Collection({
|
|
171
|
+
schema: vine.array(contributorSchema),
|
|
172
|
+
loader: contributorsLoader,
|
|
173
|
+
cache: true,
|
|
174
|
+
views: {
|
|
175
|
+
top: (contributors, limit: number = 10) =>
|
|
176
|
+
contributors.sort((a, b) => b.contributions - a.contributions).slice(0, limit),
|
|
177
|
+
},
|
|
178
|
+
})
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Custom Views
|
|
182
|
+
|
|
183
|
+
Views allow you to define reusable query methods with full type safety:
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
import vine from '@vinejs/vine'
|
|
187
|
+
import app from '@adonisjs/core/services/app'
|
|
188
|
+
import { Collection } from '@adonisjs/content'
|
|
189
|
+
import { loaders } from '@adonisjs/content/loaders'
|
|
190
|
+
|
|
191
|
+
const postSchema = vine.object({
|
|
192
|
+
title: vine.string(),
|
|
193
|
+
slug: vine.string(),
|
|
194
|
+
published: vine.boolean(),
|
|
195
|
+
publishedAt: vine.date(),
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
const posts = new Collection({
|
|
199
|
+
schema: vine.array(postSchema),
|
|
200
|
+
loader: loaders.jsonLoader(app.makePath('data/posts.json')),
|
|
201
|
+
cache: true,
|
|
202
|
+
views: {
|
|
203
|
+
// Simple filter
|
|
204
|
+
published: (posts) => posts.filter((p) => p.published),
|
|
205
|
+
|
|
206
|
+
// With parameters
|
|
207
|
+
findBySlug: (posts, slug: string) => posts.find((p) => p.slug === slug),
|
|
208
|
+
|
|
209
|
+
// Complex transformations
|
|
210
|
+
byYear: (posts) => {
|
|
211
|
+
const grouped = new Map()
|
|
212
|
+
for (const post of posts) {
|
|
213
|
+
const year = new Date(post.publishedAt).getFullYear()
|
|
214
|
+
if (!grouped.has(year)) grouped.set(year, [])
|
|
215
|
+
grouped.get(year).push(post)
|
|
216
|
+
}
|
|
217
|
+
return grouped
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
const query = await posts.load()
|
|
223
|
+
query.published() // Type-safe!
|
|
224
|
+
query.findBySlug('my-post') // Type-safe!
|
|
225
|
+
query.byYear() // Returns Map<number, Post[]>
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Caching
|
|
229
|
+
|
|
230
|
+
Collections support intelligent caching with configurable refresh schedules:
|
|
231
|
+
|
|
232
|
+
- `'daily'`: Refreshes once per day
|
|
233
|
+
- `'weekly'`: Refreshes once per week
|
|
234
|
+
- `'monthly'`: Refreshes once per month
|
|
235
|
+
|
|
236
|
+
Cached data is stored as JSON with metadata:
|
|
237
|
+
|
|
238
|
+
```json
|
|
239
|
+
{
|
|
240
|
+
"lastFetched": "2024-01-15T10:30:00.000Z",
|
|
241
|
+
"data": [...]
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Vite Integration
|
|
246
|
+
|
|
247
|
+
Use Vite asset paths in your content:
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
import vine from '@vinejs/vine'
|
|
251
|
+
import app from '@adonisjs/core/services/app'
|
|
252
|
+
import { Collection } from '@adonisjs/content'
|
|
253
|
+
import { loaders } from '@adonisjs/content/loaders'
|
|
254
|
+
|
|
255
|
+
const schema = vine.object({
|
|
256
|
+
title: vine.string(),
|
|
257
|
+
image: vine.string().toVitePath(), // Custom VineJS macro
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
const posts = new Collection({
|
|
261
|
+
schema: vine.array(schema),
|
|
262
|
+
loader: loaders.jsonLoader(app.makePath('data/posts.json')),
|
|
263
|
+
cache: true,
|
|
264
|
+
})
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
The `toVitePath()` macro converts relative paths to Vite asset paths automatically.
|
|
268
|
+
|
|
269
|
+
## Contributing
|
|
270
|
+
|
|
271
|
+
One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.
|
|
272
|
+
|
|
273
|
+
We encourage you to read the [contribution guide](https://github.com/adonisjs/.github/blob/main/docs/CONTRIBUTING.md) before contributing to the framework.
|
|
274
|
+
|
|
275
|
+
## Code of Conduct
|
|
276
|
+
|
|
277
|
+
In order to ensure that the AdonisJS community is welcoming to all, please review and abide by the [Code of Conduct](https://github.com/adonisjs/.github/blob/main/docs/CODE_OF_CONDUCT.md).
|
|
278
|
+
|
|
279
|
+
## License
|
|
280
|
+
|
|
281
|
+
@adonisjs/content is open-sourced software licensed under the [MIT license](LICENSE.md).
|
|
282
|
+
|
|
283
|
+
[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/content/checks.yml?style=for-the-badge
|
|
284
|
+
[gh-workflow-url]: https://github.com/adonisjs/content/actions/workflows/checks.yml 'Github action'
|
|
285
|
+
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
|
|
286
|
+
[typescript-url]: "typescript"
|
|
287
|
+
[npm-image]: https://img.shields.io/npm/v/@adonisjs/content.svg?style=for-the-badge&logo=npm
|
|
288
|
+
[npm-url]: https://npmjs.org/package/@adonisjs/content 'npm'
|
|
289
|
+
[license-image]: https://img.shields.io/npm/l/@adonisjs/content?color=blueviolet&style=for-the-badge
|
|
290
|
+
[license-url]: LICENSE.md 'license'
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
debug_default
|
|
3
|
+
} from "./chunk-LB6JFRVG.js";
|
|
4
|
+
|
|
5
|
+
// src/collection.ts
|
|
6
|
+
var Collection = class _Collection {
|
|
7
|
+
static #vite;
|
|
8
|
+
/** Collection configuration options */
|
|
9
|
+
#options;
|
|
10
|
+
/** Cached validated data */
|
|
11
|
+
#data;
|
|
12
|
+
#views;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new Collection instance.
|
|
15
|
+
*
|
|
16
|
+
* @param options - Configuration options including schema, loader, caching, and views
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const posts = new Collection({
|
|
21
|
+
* schema: postsSchema,
|
|
22
|
+
* loader: fileLoader,
|
|
23
|
+
* cache: true,
|
|
24
|
+
* views: { published: (posts) => posts.filter(p => p.published) }
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
constructor(options) {
|
|
29
|
+
this.#options = options;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Factory method to create a new Collection instance.
|
|
33
|
+
* This is an alternative to using the constructor directly.
|
|
34
|
+
*
|
|
35
|
+
* @param options - Configuration options including schema, loader, caching, and views
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const posts = Collection.create({
|
|
40
|
+
* schema: postsSchema,
|
|
41
|
+
* loader: fileLoader,
|
|
42
|
+
* cache: true,
|
|
43
|
+
* views: { published: (posts) => posts.filter(p => p.published) }
|
|
44
|
+
* })
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
static create(options) {
|
|
48
|
+
return new _Collection(options);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Configures the Vite service instance for resolving asset paths.
|
|
52
|
+
* This should be called once during application initialization.
|
|
53
|
+
*
|
|
54
|
+
* @param vite - The Vite service instance from @adonisjs/vite
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* Collection.useVite(vite)
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
static useVite(vite) {
|
|
62
|
+
this.#vite = vite;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Loads and validates data using the configured loader and schema.
|
|
66
|
+
* Returns cached data if caching is enabled and data was previously loaded.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const data = await posts.hydrate()
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
async hydrate() {
|
|
74
|
+
if (this.#data && this.#views && this.#options.cache) {
|
|
75
|
+
debug_default("re-using data and views from cache");
|
|
76
|
+
return {
|
|
77
|
+
data: this.#data,
|
|
78
|
+
views: this.#views
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
debug_default("computing data");
|
|
82
|
+
this.#data = await this.#options.loader.load(this.#options.schema, {
|
|
83
|
+
vite: _Collection.#vite,
|
|
84
|
+
...this.#options.validatorMetaData
|
|
85
|
+
});
|
|
86
|
+
const views = this.#options.views ?? {};
|
|
87
|
+
this.#views = Object.keys(views).reduce((result, view) => {
|
|
88
|
+
;
|
|
89
|
+
result[view] = (...args) => views[view](this.#data, ...args);
|
|
90
|
+
return result;
|
|
91
|
+
}, {});
|
|
92
|
+
return {
|
|
93
|
+
data: this.#data,
|
|
94
|
+
views: this.#views
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Loads the collection and returns a query interface with all() method
|
|
99
|
+
* and any configured view methods.
|
|
100
|
+
*
|
|
101
|
+
* The returned object includes:
|
|
102
|
+
* - all(): Returns the complete validated dataset
|
|
103
|
+
* - Custom view methods as configured in collection options
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const query = await posts.load()
|
|
108
|
+
*
|
|
109
|
+
* // Get all data
|
|
110
|
+
* const allPosts = query.all()
|
|
111
|
+
*
|
|
112
|
+
* // Use custom view methods
|
|
113
|
+
* const publishedPosts = query.published()
|
|
114
|
+
* const post = query.findBySlug('hello-world')
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
async load() {
|
|
118
|
+
const { data, views } = await this.hydrate();
|
|
119
|
+
return {
|
|
120
|
+
all() {
|
|
121
|
+
return data;
|
|
122
|
+
},
|
|
123
|
+
...views
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export {
|
|
129
|
+
Collection
|
|
130
|
+
};
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Collection
|
|
3
|
+
} from "./chunk-ZX3KSI7U.js";
|
|
4
|
+
import "./chunk-LB6JFRVG.js";
|
|
5
|
+
|
|
6
|
+
// configure.ts
|
|
7
|
+
async function configure(command) {
|
|
8
|
+
const codemods = await command.createCodemods();
|
|
9
|
+
await codemods.updateRcFile((rcFile) => {
|
|
10
|
+
rcFile.addProvider("@adonisjs/content/content_provider");
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
Collection,
|
|
15
|
+
configure
|
|
16
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type ApplicationService } from '@adonisjs/core/types';
|
|
2
|
+
declare module '@vinejs/vine' {
|
|
3
|
+
interface VineString {
|
|
4
|
+
/**
|
|
5
|
+
* Converts a relative path to a Vite asset path.
|
|
6
|
+
* This method transforms the path using Vite's asset resolution system.
|
|
7
|
+
*/
|
|
8
|
+
toVitePath(): this;
|
|
9
|
+
/**
|
|
10
|
+
* Converts a relative path to an absolute file system path.
|
|
11
|
+
* The path is resolved relative to the menu file root directory.
|
|
12
|
+
*/
|
|
13
|
+
toAbsolutePath(): this;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Service provider for the AdonisJS content package.
|
|
18
|
+
*
|
|
19
|
+
* This provider sets up the content collection system and integrates
|
|
20
|
+
* with Vite for asset handling if Vite is available in the application.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* export default {
|
|
25
|
+
* providers: [
|
|
26
|
+
* () => import('@adonisjs/content/content_provider')
|
|
27
|
+
* ]
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export default class ContentProvider {
|
|
32
|
+
protected app: ApplicationService;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new instance of the content provider.
|
|
35
|
+
*
|
|
36
|
+
* @param app - The AdonisJS application service instance
|
|
37
|
+
*/
|
|
38
|
+
constructor(app: ApplicationService);
|
|
39
|
+
/**
|
|
40
|
+
* Boots the content provider during the application boot phase.
|
|
41
|
+
*
|
|
42
|
+
* If Vite is registered in the container, this method configures
|
|
43
|
+
* the Collection class to use Vite's asset resolution system.
|
|
44
|
+
*/
|
|
45
|
+
boot(): Promise<void>;
|
|
46
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Collection
|
|
3
|
+
} from "../chunk-ZX3KSI7U.js";
|
|
4
|
+
import "../chunk-LB6JFRVG.js";
|
|
5
|
+
|
|
6
|
+
// providers/content_provider.ts
|
|
7
|
+
import { resolve } from "path";
|
|
8
|
+
import vine, { VineString } from "@vinejs/vine";
|
|
9
|
+
var toVitePath = vine.createRule(function vitePath(value, _, field) {
|
|
10
|
+
field.mutate(field.meta.vite.assetPath(value), field);
|
|
11
|
+
});
|
|
12
|
+
var toAbsolutePath = vine.createRule(function absolutePath(value, _, field) {
|
|
13
|
+
field.mutate(resolve(field.meta.menuFileRoot, value), field);
|
|
14
|
+
});
|
|
15
|
+
VineString.macro("toVitePath", function() {
|
|
16
|
+
return this.use(toVitePath());
|
|
17
|
+
});
|
|
18
|
+
VineString.macro("toAbsolutePath", function() {
|
|
19
|
+
return this.use(toAbsolutePath());
|
|
20
|
+
});
|
|
21
|
+
var ContentProvider = class {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new instance of the content provider.
|
|
24
|
+
*
|
|
25
|
+
* @param app - The AdonisJS application service instance
|
|
26
|
+
*/
|
|
27
|
+
constructor(app) {
|
|
28
|
+
this.app = app;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Boots the content provider during the application boot phase.
|
|
32
|
+
*
|
|
33
|
+
* If Vite is registered in the container, this method configures
|
|
34
|
+
* the Collection class to use Vite's asset resolution system.
|
|
35
|
+
*/
|
|
36
|
+
async boot() {
|
|
37
|
+
if (this.app.container.hasBinding("vite")) {
|
|
38
|
+
const vite = await this.app.container.make("vite");
|
|
39
|
+
Collection.useVite(vite);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
export {
|
|
44
|
+
ContentProvider as default
|
|
45
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { type Vite } from '@adonisjs/vite';
|
|
2
|
+
import { type Infer, type SchemaTypes } from '@vinejs/vine/types';
|
|
3
|
+
import { type CollectionOptions, type ViewFn, type ViewsToQueryMethods } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Manages a collection of data with schema validation and custom view functions.
|
|
6
|
+
*
|
|
7
|
+
* The Collection class provides a way to load, validate, and query structured data
|
|
8
|
+
* using VineJS schemas. It supports caching and custom view functions for data
|
|
9
|
+
* transformations and queries.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const posts = new Collection({
|
|
14
|
+
* schema: postsSchema,
|
|
15
|
+
* loader: fileLoader,
|
|
16
|
+
* cache: true,
|
|
17
|
+
* views: {
|
|
18
|
+
* published: (posts) => posts.filter(p => p.published),
|
|
19
|
+
* findBySlug: (posts, slug) => posts.find(p => p.slug === slug)
|
|
20
|
+
* }
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* const query = await posts.load()
|
|
24
|
+
* const allPosts = query.all()
|
|
25
|
+
* const publishedPosts = query.published()
|
|
26
|
+
* const post = query.findBySlug('hello-world')
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare class Collection<Schema extends SchemaTypes, Views extends Record<string, ViewFn<Schema, any, any>>> {
|
|
30
|
+
#private;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new Collection instance.
|
|
33
|
+
*
|
|
34
|
+
* @param options - Configuration options including schema, loader, caching, and views
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const posts = new Collection({
|
|
39
|
+
* schema: postsSchema,
|
|
40
|
+
* loader: fileLoader,
|
|
41
|
+
* cache: true,
|
|
42
|
+
* views: { published: (posts) => posts.filter(p => p.published) }
|
|
43
|
+
* })
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
constructor(options: CollectionOptions<Schema, Views>);
|
|
47
|
+
/**
|
|
48
|
+
* Factory method to create a new Collection instance.
|
|
49
|
+
* This is an alternative to using the constructor directly.
|
|
50
|
+
*
|
|
51
|
+
* @param options - Configuration options including schema, loader, caching, and views
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const posts = Collection.create({
|
|
56
|
+
* schema: postsSchema,
|
|
57
|
+
* loader: fileLoader,
|
|
58
|
+
* cache: true,
|
|
59
|
+
* views: { published: (posts) => posts.filter(p => p.published) }
|
|
60
|
+
* })
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
static create<Schema extends SchemaTypes, Views extends Record<string, ViewFn<Schema, any, any>>>(options: CollectionOptions<Schema, Views>): Collection<Schema, Views>;
|
|
64
|
+
/**
|
|
65
|
+
* Configures the Vite service instance for resolving asset paths.
|
|
66
|
+
* This should be called once during application initialization.
|
|
67
|
+
*
|
|
68
|
+
* @param vite - The Vite service instance from @adonisjs/vite
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* Collection.useVite(vite)
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
static useVite(vite: Vite): void;
|
|
76
|
+
/**
|
|
77
|
+
* Loads and validates data using the configured loader and schema.
|
|
78
|
+
* Returns cached data if caching is enabled and data was previously loaded.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const data = await posts.hydrate()
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
hydrate(): Promise<{
|
|
86
|
+
data: Infer<Schema> | undefined;
|
|
87
|
+
views: ViewsToQueryMethods<Views>;
|
|
88
|
+
}>;
|
|
89
|
+
/**
|
|
90
|
+
* Loads the collection and returns a query interface with all() method
|
|
91
|
+
* and any configured view methods.
|
|
92
|
+
*
|
|
93
|
+
* The returned object includes:
|
|
94
|
+
* - all(): Returns the complete validated dataset
|
|
95
|
+
* - Custom view methods as configured in collection options
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* const query = await posts.load()
|
|
100
|
+
*
|
|
101
|
+
* // Get all data
|
|
102
|
+
* const allPosts = query.all()
|
|
103
|
+
*
|
|
104
|
+
* // Use custom view methods
|
|
105
|
+
* const publishedPosts = query.published()
|
|
106
|
+
* const post = query.findBySlug('hello-world')
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
load(): Promise<{
|
|
110
|
+
all(): Infer<Schema>;
|
|
111
|
+
} & ViewsToQueryMethods<Views>>;
|
|
112
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Debug logger for the @adonisjs/content package.
|
|
3
|
+
* Enable debug logs by setting NODE_DEBUG=adonisjs:content environment variable.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* debug('loading file "%s"', filePath)
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
declare const _default: import("util").DebugLogger;
|
|
11
|
+
export default _default;
|