@stephenchenorg/astro 1.6.0 → 3.0.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/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Stephenchenorg Astro 前端通用套件
2
2
 
3
- ## 使用
3
+ [![NPM version][ico-version]][link-npm]
4
+ [![Software License][ico-license]](LICENSE)
5
+ [![Total Downloads][ico-downloads]][link-downloads]
6
+
7
+ ## 安裝
4
8
 
5
9
  ```bash
6
10
  # npm
@@ -9,6 +13,10 @@ npm install @stephenchenorg/astro
9
13
  yarn add @stephenchenorg/astro
10
14
  ```
11
15
 
16
+ ## 文件
17
+
18
+ https://stephenchenorg-astro.netlify.app
19
+
12
20
  ## 開發
13
21
 
14
22
  ```bash
@@ -16,9 +24,15 @@ yarn
16
24
  yarn build
17
25
  ```
18
26
 
19
- ## 發布新版
27
+ ## 發布套件
20
28
 
21
29
  ```bash
22
30
  npm login
23
31
  npm run release
24
32
  ```
33
+
34
+ [ico-version]: https://img.shields.io/npm/v/@stephenchenorg/astro?style=flat-square
35
+ [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen?style=flat-square
36
+ [ico-downloads]: https://img.shields.io/npm/dt/@stephenchenorg/astro?style=flat-square
37
+ [link-npm]: https://www.npmjs.com/package/@stephenchenorg/astro
38
+ [link-downloads]: https://www.npmjs.com/package/@stephenchenorg/astro
@@ -1,8 +1,13 @@
1
1
  import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ import type { APIContext } from 'astro';
2
3
  export interface CreateGraphQLAPIOptions {
3
4
  endpoint: string;
4
- defaultVariables?: Record<string, any> | (() => Record<string, any>);
5
- fetchOptions?: RequestInit | (() => RequestInit);
5
+ defaultVariables?: Record<string, any> | ((astroContext?: APIContext) => Record<string, any>);
6
+ fetchOptions?: RequestInit | ((astroContext?: APIContext) => RequestInit);
6
7
  }
7
- export declare function createGraphQLAPI(options: CreateGraphQLAPIOptions): <TData extends Record<string, any>, TVariables extends Record<string, any> = Record<string, any>>(query: TypedDocumentNode<TData, TVariables>, variables?: TVariables, fetchOptions?: RequestInit) => Promise<TData>;
8
+ export declare function createGraphQLAPI(globalOptions: CreateGraphQLAPIOptions): <TData extends Record<string, any>, TVariables extends Record<string, any> = Record<string, any>>(query: TypedDocumentNode<TData, TVariables>, options?: {
9
+ variables?: TVariables;
10
+ fetchOptions?: RequestInit;
11
+ Astro?: APIContext;
12
+ }) => Promise<TData>;
8
13
  export { gql } from 'graphql-tag';
package/dist/api/fetch.js CHANGED
@@ -1,14 +1,15 @@
1
1
  import { AwesomeGraphQLClient, GraphQLRequestError as AwesomeGraphQLRequestError } from "awesome-graphql-client";
2
2
  import { print } from "graphql/language/printer";
3
3
  import { GraphQLNotFoundError, GraphQLRequestError, GraphQLValidationError } from "./error.js";
4
- export function createGraphQLAPI(options) {
4
+ export function createGraphQLAPI(globalOptions) {
5
5
  const client = new AwesomeGraphQLClient({
6
- endpoint: options.endpoint,
6
+ endpoint: globalOptions.endpoint,
7
7
  formatQuery: (query) => print(query)
8
8
  });
9
- return function graphQLAPI(query, variables, fetchOptions) {
10
- const defaultVariables = typeof options.defaultVariables === "function" ? options.defaultVariables() : options.defaultVariables;
11
- const defaultFetchOptions = typeof options.fetchOptions === "function" ? options.fetchOptions() : options.fetchOptions;
9
+ return function graphQLAPI(query, options) {
10
+ const { variables, fetchOptions, Astro: astroContext } = options || {};
11
+ const defaultVariables = typeof globalOptions.defaultVariables === "function" ? globalOptions.defaultVariables(astroContext) : globalOptions.defaultVariables;
12
+ const defaultFetchOptions = typeof globalOptions.fetchOptions === "function" ? globalOptions.fetchOptions(astroContext) : globalOptions.fetchOptions;
12
13
  return new Promise((resolve, reject) => {
13
14
  client.request(query, {
14
15
  ...defaultVariables,
@@ -1,3 +1,3 @@
1
- export * from './fetch';
2
1
  export * from './error';
3
2
  export * from './errorResponse';
3
+ export * from './fetch';
package/dist/api/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export * from "./fetch.js";
2
1
  export * from "./error.js";
3
2
  export * from "./errorResponse.js";
3
+ export * from "./fetch.js";
@@ -1,3 +1,3 @@
1
- export * from './fragments';
2
1
  export * from './create';
2
+ export * from './fragments';
3
3
  export * from './types';
@@ -1,3 +1,3 @@
1
- export * from "./fragments.js";
2
1
  export * from "./create.js";
2
+ export * from "./fragments.js";
3
3
  export * from "./types.js";
@@ -1,7 +1,7 @@
1
- import type { PageContentField, PageField, PageImageField, PagePlainTextField, PagePlainTextareaField } from '../types';
1
+ import type { PageContentField, PageField, PageImageField, PagePlainTextareaField, PagePlainTextField } from '../types';
2
2
  export declare function isPlainTextField(field: PageField): field is PagePlainTextField;
3
3
  export declare function isPlainTextareaField(field: PageField): field is PagePlainTextareaField;
4
4
  export declare function isContentField(field: PageField): field is PageContentField;
5
5
  export declare function isImageField(field: PageField): field is PageImageField;
6
- export declare function pageTextField(fields: PageField[], key: string): any;
6
+ export declare function pageTextField(fields: PageField[], key: string): string | null;
7
7
  export declare function pageImageFieldForBackground(fields: PageField[], key: string): string;
@@ -0,0 +1,2 @@
1
+ export * from './types';
2
+ export * from './usePagination';
@@ -0,0 +1,2 @@
1
+ export * from "./types.js";
2
+ export * from "./usePagination.js";
@@ -0,0 +1,5 @@
1
+ export interface Paginator<T> {
2
+ total: number;
3
+ per_page: number;
4
+ data: T[];
5
+ }
File without changes
@@ -0,0 +1,21 @@
1
+ import type { MaybeRefOrGetter } from 'vue';
2
+ export declare function usePagination(options: {
3
+ total: MaybeRefOrGetter<number>;
4
+ currentPage: MaybeRefOrGetter<number>;
5
+ url: string;
6
+ perPage?: MaybeRefOrGetter<number>;
7
+ visiblePages?: MaybeRefOrGetter<number>;
8
+ }): {
9
+ items: import("vue").ComputedRef<number[]>;
10
+ showPagination: import("vue").ComputedRef<boolean>;
11
+ currentPage: import("vue").ComputedRef<number>;
12
+ canFirst: import("vue").ComputedRef<boolean>;
13
+ canPrev: import("vue").ComputedRef<boolean>;
14
+ canNext: import("vue").ComputedRef<boolean>;
15
+ canLast: import("vue").ComputedRef<boolean>;
16
+ firstUrl: import("vue").ComputedRef<string>;
17
+ prevUrl: import("vue").ComputedRef<string>;
18
+ nextUrl: import("vue").ComputedRef<string>;
19
+ lastUrl: import("vue").ComputedRef<string>;
20
+ getUrl: (page: number) => string;
21
+ };
@@ -0,0 +1,44 @@
1
+ import { computed, toValue } from "vue";
2
+ export function usePagination(options) {
3
+ const total = computed(() => toValue(options.total));
4
+ const currentPage = computed(() => toValue(options.currentPage));
5
+ const perPage = computed(() => toValue(options.perPage || 12));
6
+ const totalPages = computed(() => Math.ceil(total.value / perPage.value));
7
+ const visiblePages = computed(() => Math.min(toValue(options.visiblePages || 5), totalPages.value));
8
+ const sideCount = computed(() => Math.floor(visiblePages.value / 2));
9
+ const items = computed(() => {
10
+ const items2 = [];
11
+ let start = Math.max(1, currentPage.value - sideCount.value);
12
+ let end = Math.min(totalPages.value, currentPage.value + sideCount.value);
13
+ if (end - start + 1 < visiblePages.value && currentPage.value > 0) {
14
+ if (currentPage.value <= sideCount.value) {
15
+ end = Math.min(totalPages.value, start + visiblePages.value - 1);
16
+ } else if (currentPage.value > totalPages.value - sideCount.value) {
17
+ start = Math.max(1, end - visiblePages.value + 1);
18
+ }
19
+ }
20
+ for (let i = start; i <= end; i++) {
21
+ items2.push(i);
22
+ }
23
+ return items2;
24
+ });
25
+ function getUrl(page) {
26
+ const url = new URL(options.url);
27
+ url.searchParams.set("page", String(page));
28
+ return url.toString();
29
+ }
30
+ return {
31
+ items,
32
+ showPagination: computed(() => total.value > perPage.value),
33
+ currentPage,
34
+ canFirst: computed(() => currentPage.value > 1),
35
+ canPrev: computed(() => currentPage.value > 1),
36
+ canNext: computed(() => currentPage.value < totalPages.value),
37
+ canLast: computed(() => currentPage.value < totalPages.value),
38
+ firstUrl: computed(() => getUrl(1)),
39
+ prevUrl: computed(() => getUrl(currentPage.value - 1)),
40
+ nextUrl: computed(() => getUrl(currentPage.value + 1)),
41
+ lastUrl: computed(() => getUrl(totalPages.value)),
42
+ getUrl
43
+ };
44
+ }
@@ -1,7 +1,7 @@
1
1
  import ProvideUrlConfig from './components/ProvideUrlConfig.astro';
2
2
  export * from './config';
3
3
  export * from './store';
4
+ export * from './types';
4
5
  export * from './url';
5
6
  export * from './utils';
6
- export * from './types';
7
7
  export { ProvideUrlConfig };
@@ -1,7 +1,7 @@
1
1
  import ProvideUrlConfig from "./components/ProvideUrlConfig.astro";
2
2
  export * from "./config.js";
3
3
  export * from "./store.js";
4
+ export * from "./types.js";
4
5
  export * from "./url.js";
5
6
  export * from "./utils.js";
6
- export * from "./types.js";
7
7
  export { ProvideUrlConfig };
@@ -1,5 +1,5 @@
1
- import qs from 'query-string';
2
1
  import type { UrlConfig } from './types';
2
+ import qs from 'query-string';
3
3
  declare global {
4
4
  interface Window {
5
5
  __astro_provide_url_config__?: UrlConfig;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stephenchenorg/astro",
3
3
  "type": "module",
4
- "version": "1.6.0",
4
+ "version": "3.0.0",
5
5
  "description": "Stephenchenorg Astro 前端通用套件",
6
6
  "license": "MIT",
7
7
  "homepage": "https://stephenchenorg-astro.netlify.app",
@@ -29,9 +29,13 @@
29
29
  "types": "./dist/page/index.d.ts",
30
30
  "import": "./dist/page/index.js"
31
31
  },
32
- "./pagination": {
33
- "types": "./dist/pagination/index.d.ts",
34
- "import": "./dist/pagination/index.js"
32
+ "./pagination-astro": {
33
+ "types": "./dist/pagination-astro/index.d.ts",
34
+ "import": "./dist/pagination-astro/index.js"
35
+ },
36
+ "./pagination-vue": {
37
+ "types": "./dist/pagination-vue/index.d.ts",
38
+ "import": "./dist/pagination-vue/index.js"
35
39
  },
36
40
  "./query-params": {
37
41
  "types": "./dist/query-params/index.d.ts",
@@ -55,31 +59,38 @@
55
59
  "node": ">=22"
56
60
  },
57
61
  "scripts": {
58
- "build": "mkdist --declaration --ext=js",
62
+ "build": "mkdist --declaration --ext=js && sh ./scripts/postbuild.sh",
59
63
  "lint": "eslint \"*.{js,ts,json}\" \"src/**/*.ts\"",
60
64
  "code-check": "astro check && npm run lint",
61
65
  "prepack": "npm run build",
62
66
  "release": "bumpp --commit \"Release v%s\" && npm publish"
63
67
  },
64
68
  "peerDependencies": {
65
- "astro": "^5.0.0"
69
+ "astro": "^5.0.0",
70
+ "vue": "^3.3.0"
71
+ },
72
+ "peerDependenciesMeta": {
73
+ "vue": {
74
+ "optional": true
75
+ }
66
76
  },
67
77
  "dependencies": {
68
78
  "@graphql-typed-document-node/core": "^3.2.0",
69
79
  "awesome-graphql-client": "^2.1.0",
70
- "graphql": "^16.10.0",
80
+ "graphql": "^16.11.0",
71
81
  "graphql-tag": "^2.12.6",
72
82
  "nanostores": "^1.0.1",
73
- "query-string": "^9.1.2"
83
+ "query-string": "^9.2.0"
74
84
  },
75
85
  "devDependencies": {
76
86
  "@astrojs/check": "^0.9.4",
77
- "@ycs77/eslint-config": "^4.1.0",
78
- "astro": "^5.7.10",
79
- "bumpp": "^10.1.0",
80
- "eslint": "^9.26.0",
87
+ "@ycs77/eslint-config": "^4.3.0",
88
+ "astro": "^5.9.1",
89
+ "bumpp": "^10.1.1",
90
+ "eslint": "^9.28.0",
81
91
  "eslint-plugin-astro": "^1.3.1",
82
92
  "mkdist": "^2.3.0",
83
- "typescript": "~5.8.3"
93
+ "typescript": "^5.8.3",
94
+ "vue": "^3.5.16"
84
95
  }
85
96
  }
@@ -1,45 +0,0 @@
1
- ---
2
- import { usePagination } from '@stephenchenorg/astro/pagination'
3
-
4
- interface Props {
5
- total: number
6
- perPage?: number
7
- visiblePages?: number
8
- currentPage?: number
9
- }
10
-
11
- const {
12
- items,
13
- showPagination,
14
- currentPage,
15
- canFirst,
16
- canPrev,
17
- canNext,
18
- canLast,
19
- firstUrl,
20
- prevUrl,
21
- nextUrl,
22
- lastUrl,
23
- getUrl,
24
- } = usePagination({
25
- total: Astro.props.total,
26
- perPage: Astro.props.perPage,
27
- visiblePages: Astro.props.visiblePages,
28
- currentPage: Astro.props.currentPage || Number(Astro.url.searchParams.get('page')) || 1,
29
- url: Astro.request.url,
30
- })
31
- ---
32
-
33
- {showPagination && (
34
- <div>
35
- {canFirst && <a href={firstUrl}>First</a>}
36
- {canPrev && <a href={prevUrl}>Previous</a>}
37
- {items.map(page =>
38
- page === currentPage
39
- ? <span>{page}</span>
40
- : <a href={getUrl(page)}>{page}</a>
41
- )}
42
- {canNext && <a href={nextUrl}>Next</a>}
43
- {canLast && <a href={lastUrl}>Last</a>}
44
- </div>
45
- )}
@@ -1,2 +1,2 @@
1
- export * from './usePagination';
2
1
  export * from './types';
2
+ export * from './usePagination';
@@ -1,2 +1,2 @@
1
- export * from "./usePagination.js";
2
1
  export * from "./types.js";
2
+ export * from "./usePagination.js";
File without changes