@newfold/huapi-js 1.1.1 → 1.1.3

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 ADDED
@@ -0,0 +1,32 @@
1
+ # @newfold/huapi-js
2
+
3
+ > A JavaScript Client Library for HUAPI
4
+
5
+ ## Installation
6
+
7
+ TODO
8
+
9
+ ## Usage
10
+
11
+ ### React Hooks
12
+
13
+ ```js
14
+ import React from "react";
15
+
16
+ import { useGetHostingSites } from "@newfold/huapi-js";
17
+
18
+ const MyComponent = () => {
19
+ const hostingId = "2";
20
+ const { isLoading, data } = useGetHostingSites(hostingId);
21
+
22
+ if (isLoading) return <h1>Loading...</h1>;
23
+
24
+ return (
25
+ <div>
26
+ {data?.data?.rows?.map((site) => {
27
+ <div key={site.id}>{site.name}</div>;
28
+ })}
29
+ </div>
30
+ );
31
+ };
32
+ ```
package/openapi.json CHANGED
@@ -11,9 +11,26 @@
11
11
  "type": "object",
12
12
  "description": "A site",
13
13
  "properties": {
14
- "id": { "type": "string", "description": "TODO" },
15
- "name": { "type": "string", "description": "TODO" },
16
- "url": { "type": "string", "description": "TODO" }
14
+ "id": {
15
+ "type": "integer",
16
+ "description": "TODO",
17
+ "example": 1
18
+ },
19
+ "name": {
20
+ "type": "string",
21
+ "description": "TODO",
22
+ "example": "Example Site 1"
23
+ },
24
+ "url": {
25
+ "type": "string",
26
+ "description": "TODO",
27
+ "example": "https://example.com"
28
+ }
29
+ },
30
+ "example": {
31
+ "id": 1,
32
+ "name": "Example Site 1",
33
+ "url": "https://example.com"
17
34
  },
18
35
  "required": ["id", "name", "url"]
19
36
  },
@@ -21,19 +38,68 @@
21
38
  "description": "List of sites",
22
39
  "type": "object",
23
40
  "properties": {
24
- "limit": { "type": "string", "description": "TODO" },
25
- "n_pages": { "type": "string", "description": "TODO" },
26
- "n_rows": { "type": "string", "description": "TODO" },
27
- "page": { "type": "string", "description": "TODO" },
41
+ "limit": {
42
+ "type": "integer",
43
+ "description": "Max number of sites included in response",
44
+ "example": 10
45
+ },
46
+ "n_pages": {
47
+ "type": "integer",
48
+ "description": "TODO",
49
+ "example": 1
50
+ },
51
+ "n_rows": {
52
+ "type": "integer",
53
+ "description": "TODO",
54
+ "example": 1
55
+ },
56
+ "page": {
57
+ "type": "integer",
58
+ "description": "TODO",
59
+ "example": 1
60
+ },
28
61
  "rows": {
29
62
  "type": "array",
30
63
  "description": "TODO",
31
64
  "items": {
32
65
  "type": "array",
33
66
  "$ref": "#/components/schemas/Site"
34
- }
67
+ },
68
+ "example": [
69
+ {
70
+ "id": 1,
71
+ "name": "Example Site 1",
72
+ "url": "https://example.com"
73
+ },
74
+ {
75
+ "id": 2,
76
+ "name": "Example Site 2",
77
+ "url": "https://example2.com"
78
+ }
79
+ ]
35
80
  }
36
- }
81
+ },
82
+ "example": {
83
+ "limit": 10,
84
+ "n_pages": 1,
85
+ "n_rows": 1,
86
+ "page": 1,
87
+ "rows": [
88
+ [
89
+ {
90
+ "id": 1,
91
+ "name": "Example Site 1",
92
+ "url": "https://example.com"
93
+ },
94
+ {
95
+ "id": 2,
96
+ "name": "Example Site 2",
97
+ "url": "https://example2.com"
98
+ }
99
+ ]
100
+ ]
101
+ },
102
+ "required": ["limit", "n_pages", "n_rows", "page", "rows"]
37
103
  },
38
104
  "SiteSso": {
39
105
  "description": "SSO token or login URL",
@@ -45,6 +111,9 @@
45
111
  "description": "TODO"
46
112
  }
47
113
  },
114
+ "example": {
115
+ "sso": "$randomUrl"
116
+ },
48
117
  "required": ["sso"]
49
118
  }
50
119
  },
@@ -63,7 +132,11 @@
63
132
  "operationId": "site_sso",
64
133
  "description": "TODO",
65
134
  "tags": ["sites"],
66
- "security": [{ "bearerAuth": [] }],
135
+ "security": [
136
+ {
137
+ "bearerAuth": []
138
+ }
139
+ ],
67
140
  "parameters": [
68
141
  {
69
142
  "name": "site_id",
@@ -83,9 +156,6 @@
83
156
  "application/json": {
84
157
  "schema": {
85
158
  "$ref": "#/components/schemas/SiteSso"
86
- },
87
- "example": {
88
- "sso": "$randomUrl"
89
159
  }
90
160
  }
91
161
  }
@@ -118,19 +188,6 @@
118
188
  "application/json": {
119
189
  "schema": {
120
190
  "$ref": "#/components/schemas/SiteList"
121
- },
122
- "example": {
123
- "limit": "10",
124
- "n_pages": "1",
125
- "n_rows": "1",
126
- "page": "1",
127
- "rows": [
128
- {
129
- "id": "1",
130
- "name": "Example Site",
131
- "url": "https://example.com"
132
- }
133
- ]
134
191
  }
135
192
  }
136
193
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newfold/huapi-js",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "main": "src/huapi.js",
5
5
  "license": "MIT",
6
6
  "devDependencies": {
@@ -17,7 +17,7 @@
17
17
  "axios": "^0.26.1",
18
18
  "msw": "^0.39.2",
19
19
  "orval": "^6.7.1",
20
- "react": "^18.0.0",
20
+ "react": "^17.0.2",
21
21
  "react-query": "^3.34.19"
22
22
  },
23
23
  "scripts": {
package/src/huapi.js CHANGED
@@ -70,7 +70,7 @@ var useGetHostingSites = function (hostingId, options) {
70
70
  exports.useGetHostingSites = useGetHostingSites;
71
71
  var getSiteSsoMock = function () { return ({ sso: faker_1.faker.random.word() }); };
72
72
  exports.getSiteSsoMock = getSiteSsoMock;
73
- var getGetHostingSitesMock = function () { return ({ limit: faker_1.faker.helpers.randomize([faker_1.faker.random.word(), undefined]), n_pages: faker_1.faker.helpers.randomize([faker_1.faker.random.word(), undefined]), n_rows: faker_1.faker.helpers.randomize([faker_1.faker.random.word(), undefined]), page: faker_1.faker.helpers.randomize([faker_1.faker.random.word(), undefined]), rows: faker_1.faker.helpers.randomize([__spreadArray([], Array(faker_1.faker.datatype.number({ min: 1, max: 10 })), true).map(function () { return ({ id: faker_1.faker.random.word(), name: faker_1.faker.random.word(), url: faker_1.faker.random.word() }); }), undefined]) }); };
73
+ var getGetHostingSitesMock = function () { return ({ limit: faker_1.faker.datatype.number(), n_pages: faker_1.faker.datatype.number(), n_rows: faker_1.faker.datatype.number(), page: faker_1.faker.datatype.number(), rows: __spreadArray([], Array(faker_1.faker.datatype.number({ min: 1, max: 10 })), true).map(function () { return ({ id: faker_1.faker.datatype.number(), name: faker_1.faker.random.word(), url: faker_1.faker.random.word() }); }) }); };
74
74
  exports.getGetHostingSitesMock = getGetHostingSitesMock;
75
75
  var getHostingUAPIMSW = function () { return [
76
76
  msw_1.rest.post('*/sites/:siteid/sso', function (_req, res, ctx) {
package/src/huapi.ts ADDED
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Generated by orval v6.7.1 🍺
3
+ * Do not edit manually.
4
+ * Hosting UAPI
5
+ * Hosting UAPI
6
+ * OpenAPI spec version: 0.1.0
7
+ */
8
+ import axios,{
9
+ AxiosRequestConfig,
10
+ AxiosResponse,
11
+ AxiosError
12
+ } from 'axios'
13
+ import {
14
+ useQuery,
15
+ useMutation,
16
+ UseQueryOptions,
17
+ UseMutationOptions,
18
+ QueryFunction,
19
+ MutationFunction,
20
+ UseQueryResult,
21
+ QueryKey
22
+ } from 'react-query'
23
+ import {
24
+ rest
25
+ } from 'msw'
26
+ import {
27
+ faker
28
+ } from '@faker-js/faker'
29
+ /**
30
+ * SSO token or login URL
31
+ */
32
+ export interface SiteSso {
33
+ /** TODO */
34
+ sso: string;
35
+ }
36
+
37
+ /**
38
+ * A site
39
+ */
40
+ export interface Site {
41
+ /** TODO */
42
+ id: number;
43
+ /** TODO */
44
+ name: string;
45
+ /** TODO */
46
+ url: string;
47
+ }
48
+
49
+ /**
50
+ * List of sites
51
+ */
52
+ export interface SiteList {
53
+ /** Max number of sites included in response */
54
+ limit: number;
55
+ /** TODO */
56
+ n_pages: number;
57
+ /** TODO */
58
+ n_rows: number;
59
+ /** TODO */
60
+ page: number;
61
+ /** TODO */
62
+ rows: Site[];
63
+ }
64
+
65
+
66
+
67
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
68
+ type AsyncReturnType<
69
+ T extends (...args: any) => Promise<any>
70
+ > = T extends (...args: any) => Promise<infer R> ? R : any;
71
+
72
+
73
+ /**
74
+ * TODO
75
+ * @summary Get SSO token
76
+ */
77
+ export const siteSso = (
78
+ siteId: number, options?: AxiosRequestConfig
79
+ ): Promise<AxiosResponse<SiteSso>> => {
80
+ return axios.post(
81
+ `/sites/${siteId}/sso`,undefined,options
82
+ );
83
+ }
84
+
85
+
86
+
87
+ export type SiteSsoMutationResult = NonNullable<AsyncReturnType<typeof siteSso>>
88
+
89
+ export type SiteSsoMutationError = AxiosError<unknown>
90
+
91
+ export const useSiteSso = <TError = AxiosError<unknown>,
92
+
93
+ TContext = unknown>(options?: { mutation?:UseMutationOptions<AsyncReturnType<typeof siteSso>, TError,{siteId: number}, TContext>, axios?: AxiosRequestConfig}
94
+ ) => {
95
+ const {mutation: mutationOptions, axios: axiosOptions} = options || {}
96
+
97
+
98
+
99
+
100
+ const mutationFn: MutationFunction<AsyncReturnType<typeof siteSso>, {siteId: number}> = (props) => {
101
+ const {siteId} = props || {};
102
+
103
+ return siteSso(siteId,axiosOptions)
104
+ }
105
+
106
+ return useMutation<AsyncReturnType<typeof siteSso>, TError, {siteId: number}, TContext>(mutationFn, mutationOptions)
107
+ }
108
+
109
+ /**
110
+ * Obtain a list of sites.
111
+ * @summary Get sites for a hosting id
112
+ */
113
+ export const getHostingSites = (
114
+ hostingId: number, options?: AxiosRequestConfig
115
+ ): Promise<AxiosResponse<SiteList>> => {
116
+ return axios.get(
117
+ `/hosting/${hostingId}/sites`,options
118
+ );
119
+ }
120
+
121
+
122
+ export const getGetHostingSitesQueryKey = (hostingId: number,) => [`/hosting/${hostingId}/sites`];
123
+
124
+
125
+ export type GetHostingSitesQueryResult = NonNullable<AsyncReturnType<typeof getHostingSites>>
126
+ export type GetHostingSitesQueryError = AxiosError<unknown>
127
+
128
+ export const useGetHostingSites = <TData = AsyncReturnType<typeof getHostingSites>, TError = AxiosError<unknown>>(
129
+ hostingId: number, options?: { query?:UseQueryOptions<AsyncReturnType<typeof getHostingSites>, TError, TData>, axios?: AxiosRequestConfig}
130
+
131
+ ): UseQueryResult<TData, TError> & { queryKey: QueryKey } => {
132
+
133
+ const {query: queryOptions, axios: axiosOptions} = options || {}
134
+
135
+ const queryKey = queryOptions?.queryKey ?? getGetHostingSitesQueryKey(hostingId);
136
+
137
+
138
+
139
+ const queryFn: QueryFunction<AsyncReturnType<typeof getHostingSites>> = () => getHostingSites(hostingId, axiosOptions);
140
+
141
+ const query = useQuery<AsyncReturnType<typeof getHostingSites>, TError, TData>(queryKey, queryFn, {enabled: !!(hostingId), ...queryOptions})
142
+
143
+ return {
144
+ queryKey,
145
+ ...query
146
+ }
147
+ }
148
+
149
+
150
+
151
+
152
+ export const getSiteSsoMock = () => ({sso: faker.random.word()})
153
+
154
+ export const getGetHostingSitesMock = () => ({limit: faker.datatype.number(), n_pages: faker.datatype.number(), n_rows: faker.datatype.number(), page: faker.datatype.number(), rows: [...Array(faker.datatype.number({min: 1, max: 10}))].map(() => ({id: faker.datatype.number(), name: faker.random.word(), url: faker.random.word()}))})
155
+
156
+ export const getHostingUAPIMSW = () => [
157
+ rest.post('*/sites/:siteid/sso', (_req, res, ctx) => {
158
+ return res(
159
+ ctx.delay(1000),
160
+ ctx.status(200, 'Mocked status'),
161
+ ctx.json(getSiteSsoMock()),
162
+ )
163
+ }),rest.get('*/hosting/:hostingid/sites', (_req, res, ctx) => {
164
+ return res(
165
+ ctx.delay(1000),
166
+ ctx.status(200, 'Mocked status'),
167
+ ctx.json(getGetHostingSitesMock()),
168
+ )
169
+ }),]