@gravito/satellite-ad 0.1.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # @gravito/satellite-ad
2
+
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @gravito/atlas@1.0.1
9
+ - @gravito/core@1.0.0
10
+ - @gravito/enterprise@1.0.0
@@ -0,0 +1,8 @@
1
+ import { ServiceProvider, Container } from '@gravito/core';
2
+
3
+ declare class AdServiceProvider extends ServiceProvider {
4
+ register(_container: Container): void;
5
+ boot(): void;
6
+ }
7
+
8
+ export { AdServiceProvider };
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ // src/index.ts
2
+ import { ServiceProvider } from "@gravito/core";
3
+ var AdServiceProvider = class extends ServiceProvider {
4
+ register(_container) {
5
+ }
6
+ boot() {
7
+ const core = this.core;
8
+ if (!core) {
9
+ return;
10
+ }
11
+ core.logger.info("\u{1F3AF} Ad Satellite is calculating campaign weights");
12
+ core.router.prefix("/api/admin/v1/ads").group((router) => {
13
+ router.get(
14
+ "/",
15
+ (ctx) => ctx.json([
16
+ {
17
+ id: "ad-1",
18
+ title: "\u590F\u5B63\u5927\u4FC3\u92B7",
19
+ slotSlug: "HOME_HERO",
20
+ imageUrl: "https://images.unsplash.com/photo-1607082348824-0a96f2a4b9da",
21
+ status: "ACTIVE",
22
+ weight: 5,
23
+ startsAt: "2025-06-01",
24
+ endsAt: "2025-08-31"
25
+ },
26
+ {
27
+ id: "ad-2",
28
+ title: "\u514D\u904B\u512A\u60E0\u4E2D",
29
+ slotSlug: "HOME_HERO",
30
+ imageUrl: "https://images.unsplash.com/photo-1557821552-17105176677c",
31
+ status: "ACTIVE",
32
+ weight: 10,
33
+ startsAt: "2025-01-01",
34
+ endsAt: "2025-12-31"
35
+ }
36
+ ])
37
+ );
38
+ });
39
+ core.router.get("/api/v1/ads/:slot", (ctx) => {
40
+ const slot = ctx.req.param("slot");
41
+ return ctx.json({
42
+ id: "ad-picked",
43
+ title: "\u96A8\u6A5F\u63A8\u9078\u5EE3\u544A",
44
+ imageUrl: "https://images.unsplash.com/photo-1607082348824-0a96f2a4b9da",
45
+ targetUrl: "/promotions/summer-sale",
46
+ slot
47
+ });
48
+ });
49
+ }
50
+ };
51
+ export {
52
+ AdServiceProvider
53
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@gravito/satellite-ad",
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsup src/index.ts --format esm --dts",
8
+ "typecheck": "bun tsc -p tsconfig.json --noEmit --skipLibCheck"
9
+ },
10
+ "types": "dist/index.d.ts",
11
+ "dependencies": {
12
+ "@gravito/enterprise": "workspace:*",
13
+ "@gravito/atlas": "workspace:*",
14
+ "@gravito/core": "workspace:*"
15
+ },
16
+ "devDependencies": {
17
+ "tsup": "^8.3.5",
18
+ "typescript": "^5.9.3"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/gravito-framework/gravito.git",
26
+ "directory": "satellites/ad"
27
+ }
28
+ }
@@ -0,0 +1,26 @@
1
+ import { Entity } from '@gravito/enterprise'
2
+
3
+ export interface AdProps {
4
+ slotSlug: string
5
+ title: string
6
+ imageUrl: string
7
+ targetUrl: string
8
+ weight: number // 1-10
9
+ status: 'ACTIVE' | 'PAUSED'
10
+ startsAt: Date
11
+ endsAt: Date
12
+ createdAt: Date
13
+ }
14
+
15
+ export class Advertisement extends Entity<string> {
16
+ private _props: AdProps
17
+
18
+ constructor(props: AdProps, id?: string) {
19
+ super(id || crypto.randomUUID())
20
+ this._props = props
21
+ }
22
+
23
+ unpack() {
24
+ return { ...this._props }
25
+ }
26
+ }
package/src/index.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { type Container, ServiceProvider } from '@gravito/core'
2
+
3
+ export class AdServiceProvider extends ServiceProvider {
4
+ register(_container: Container): void {
5
+ // 註冊服務
6
+ }
7
+
8
+ override boot(): void {
9
+ const core = this.core
10
+ if (!core) {
11
+ return
12
+ }
13
+
14
+ core.logger.info('🎯 Ad Satellite is calculating campaign weights')
15
+
16
+ core.router.prefix('/api/admin/v1/ads').group((router) => {
17
+ router.get('/', (ctx) =>
18
+ ctx.json([
19
+ {
20
+ id: 'ad-1',
21
+ title: '夏季大促銷',
22
+ slotSlug: 'HOME_HERO',
23
+ imageUrl: 'https://images.unsplash.com/photo-1607082348824-0a96f2a4b9da',
24
+ status: 'ACTIVE',
25
+ weight: 5,
26
+ startsAt: '2025-06-01',
27
+ endsAt: '2025-08-31',
28
+ },
29
+ {
30
+ id: 'ad-2',
31
+ title: '免運優惠中',
32
+ slotSlug: 'HOME_HERO',
33
+ imageUrl: 'https://images.unsplash.com/photo-1557821552-17105176677c',
34
+ status: 'ACTIVE',
35
+ weight: 10,
36
+ startsAt: '2025-01-01',
37
+ endsAt: '2025-12-31',
38
+ },
39
+ ])
40
+ )
41
+ })
42
+
43
+ // 公開 API: 根據版位獲取隨機廣告
44
+ core.router.get('/api/v1/ads/:slot', (ctx) => {
45
+ const slot = ctx.req.param('slot')
46
+ // 模擬隨機投放邏輯 (這裡會根據 Weight 進行隨機抽選)
47
+ return ctx.json({
48
+ id: 'ad-picked',
49
+ title: '隨機推選廣告',
50
+ imageUrl: 'https://images.unsplash.com/photo-1607082348824-0a96f2a4b9da',
51
+ targetUrl: '/promotions/summer-sale',
52
+ slot,
53
+ })
54
+ })
55
+ }
56
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "baseUrl": ".",
6
+ "skipLibCheck": true,
7
+ "types": ["bun-types"],
8
+ "paths": {
9
+ "@gravito/core": ["../../packages/core/src/index.ts"],
10
+ "@gravito/*": ["../../packages/*/src/index.ts"]
11
+ }
12
+ },
13
+ "include": [
14
+ "src/**/*"
15
+ ],
16
+ "exclude": [
17
+ "node_modules",
18
+ "dist",
19
+ "**/*.test.ts"
20
+ ]
21
+ }