@domandigital/gbp 0.3.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 +202 -0
- package/README.md +145 -0
- package/dist/index.cjs +144 -0
- package/dist/index.d.cts +91 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.js +114 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Doman Digital Ltd
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# @domandigital/gbp
|
|
2
|
+
|
|
3
|
+
A zero-dependency Google Business Profile client for Doman Digital / IDS
|
|
4
|
+
client sites: OAuth refresh-token auth and a paginated reviews fetch,
|
|
5
|
+
including the business's own reply to each review.
|
|
6
|
+
|
|
7
|
+
## Why this exists
|
|
8
|
+
|
|
9
|
+
Extracted from MMM Beauty's `packages/google-apis` (`business-profile.ts` +
|
|
10
|
+
`oauth-client.ts`), which was already proven in production against the
|
|
11
|
+
real GBP API's quirks -- star-rating enums, page-size caps, graceful
|
|
12
|
+
degrade-to-empty when unconfigured. Six client sites need this, not one, so
|
|
13
|
+
it moved out into its own package rather than being copy-pasted five more
|
|
14
|
+
times. That's the exact drift `@domandigital/graph` already exists to
|
|
15
|
+
prevent, one layer down: OAuth + pagination + enum-decoding logic
|
|
16
|
+
independently reinvented per repo instead of fixed once.
|
|
17
|
+
|
|
18
|
+
This package does **not** depend on `@domandigital/graph` and vice versa.
|
|
19
|
+
`gbp` fetches review facts; `graph` turns facts into JSON-LD. A consuming
|
|
20
|
+
site composes them at the call site:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { getBusinessReviews } from "@domandigital/gbp";
|
|
24
|
+
import { buildReview, createGraphIds } from "@domandigital/graph";
|
|
25
|
+
|
|
26
|
+
const { reviews, averageRating, totalReviewCount } = await getBusinessReviews();
|
|
27
|
+
const reviewNodes = reviews.map((r) =>
|
|
28
|
+
buildReview(
|
|
29
|
+
{
|
|
30
|
+
authorName: r.author,
|
|
31
|
+
reviewBody: r.comment,
|
|
32
|
+
ratingValue: r.rating,
|
|
33
|
+
datePublished: r.createdAt,
|
|
34
|
+
reply: r.reply ? { text: r.reply.text, dateCreated: r.reply.updatedAt } : null,
|
|
35
|
+
},
|
|
36
|
+
ids,
|
|
37
|
+
),
|
|
38
|
+
);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`averageRating` / `totalReviewCount` feed `OrganizationInput.aggregateRating`
|
|
42
|
+
directly -- don't hardcode a rating/count literal in a site's settings file
|
|
43
|
+
once this is wired in. That literal drifting from the real listing (a site
|
|
44
|
+
claiming 7 reviews when Google has 6) is the bug this package exists to kill.
|
|
45
|
+
|
|
46
|
+
## Why Business Profile API, not Places API
|
|
47
|
+
|
|
48
|
+
Places API's `Review` object has no field for the business's reply, full
|
|
49
|
+
stop -- and caps out at 5 reviews per place with no pagination. Neither
|
|
50
|
+
limitation applies here: this uses the Business Profile API (the same one
|
|
51
|
+
you already manage the listing through), which returns every review, paginated,
|
|
52
|
+
with the reply attached, for free.
|
|
53
|
+
|
|
54
|
+
## Install
|
|
55
|
+
|
|
56
|
+
Not published to npm. Consumed as a git dependency pinned to a tag:
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@domandigital/gbp": "github:Doman-Digital/dd-gbp#v0.1.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`dist/` is committed to this repo (no CI build step runs on a git-dependency
|
|
67
|
+
install), so no build step is required in the consuming project beyond a
|
|
68
|
+
normal `pnpm install`.
|
|
69
|
+
|
|
70
|
+
## Env
|
|
71
|
+
|
|
72
|
+
| Var | Purpose |
|
|
73
|
+
|---|---|
|
|
74
|
+
| `GBP_CLIENT_ID` | Google Cloud OAuth client id |
|
|
75
|
+
| `GBP_CLIENT_SECRET` | Google Cloud OAuth client secret |
|
|
76
|
+
| `GBP_REFRESH_TOKEN` | User refresh token (service accounts are rejected by this API) |
|
|
77
|
+
| `GOOGLE_BUSINESS_ACCOUNT_ID` | e.g. `accounts/1234567890` |
|
|
78
|
+
| `GOOGLE_BUSINESS_LOCATION_ID` | e.g. `locations/9876543210` |
|
|
79
|
+
|
|
80
|
+
Which actual Google Cloud OAuth client + refresh token you point at (the
|
|
81
|
+
agency's shared credential, or a business's own dedicated one) is entirely a
|
|
82
|
+
deployment-env decision, not a code-level one -- each app configures its own.
|
|
83
|
+
|
|
84
|
+
## API
|
|
85
|
+
|
|
86
|
+
### `getBusinessReviews(options?): Promise<BusinessReviewsResult>`
|
|
87
|
+
|
|
88
|
+
Paginates through every review (GBP caps each page at 50) and returns:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
interface BusinessReviewsResult {
|
|
92
|
+
averageRating: number | null;
|
|
93
|
+
totalReviewCount: number;
|
|
94
|
+
reviews: BusinessReview[];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface BusinessReview {
|
|
98
|
+
id: string;
|
|
99
|
+
author: string;
|
|
100
|
+
authorPhoto?: string;
|
|
101
|
+
rating: number; // 1-5
|
|
102
|
+
comment: string;
|
|
103
|
+
createdAt: string; // ISO
|
|
104
|
+
reply?: { text: string; updatedAt: string }; // no author field -- see below
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Options:
|
|
109
|
+
|
|
110
|
+
- `limit?: number` -- stop paginating once this many reviews are collected.
|
|
111
|
+
Omit to fetch all of them.
|
|
112
|
+
- `filterMinStars?: number` -- drop reviews below this rating. Use for a
|
|
113
|
+
public testimonial feed; omit for an owner-facing surface where the point
|
|
114
|
+
is to see everything. `totalReviewCount` always reflects the location's
|
|
115
|
+
real total, never the filtered count.
|
|
116
|
+
- `next?: { revalidate?: number; tags?: string[] }` -- passed straight through
|
|
117
|
+
to `fetch`'s Next.js cache-hint augmentation. A no-op outside Next.js.
|
|
118
|
+
Default: revalidate hourly.
|
|
119
|
+
|
|
120
|
+
Never throws on missing configuration -- `isBusinessProfileConfigured()` gates
|
|
121
|
+
internally and returns an empty result, so UI can render unconditionally.
|
|
122
|
+
Does throw on a real API failure (non-OK response), so a calling route should
|
|
123
|
+
catch and degrade explicitly if it wants zero-downtime behavior on a Google
|
|
124
|
+
outage.
|
|
125
|
+
|
|
126
|
+
### Why a reply has no author field
|
|
127
|
+
|
|
128
|
+
A GBP review has exactly one reply slot, and it can only ever come from the
|
|
129
|
+
business -- there is no third party to disambiguate, so the API returns
|
|
130
|
+
`{ comment, updateTime, reviewReplyState }` with nothing else. Google's own
|
|
131
|
+
listing UI renders every reply as "Response from the owner" for the same
|
|
132
|
+
reason. Attribute it to the business's own Organization node in the
|
|
133
|
+
consumer (see the `@domandigital/graph` composition example above) --
|
|
134
|
+
don't go looking for an author field that doesn't exist.
|
|
135
|
+
|
|
136
|
+
### `isBusinessProfileConfigured(): boolean`
|
|
137
|
+
|
|
138
|
+
True once all five env vars above are set.
|
|
139
|
+
|
|
140
|
+
### `hasGoogleOAuthCredentials()` / `getGoogleOAuthAccessToken()`
|
|
141
|
+
|
|
142
|
+
Lower-level OAuth primitives, exported in case a consumer needs the raw
|
|
143
|
+
access token for another Business Profile endpoint this package doesn't wrap
|
|
144
|
+
(e.g. business hours, Q&A). The token is cached in-process and refreshed a
|
|
145
|
+
minute before expiry.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
getBusinessReviews: () => getBusinessReviews,
|
|
24
|
+
getGoogleOAuthAccessToken: () => getGoogleOAuthAccessToken,
|
|
25
|
+
hasGoogleOAuthCredentials: () => hasGoogleOAuthCredentials,
|
|
26
|
+
isBusinessProfileConfigured: () => isBusinessProfileConfigured
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/oauth-client.ts
|
|
31
|
+
var TOKEN_URI = "https://oauth2.googleapis.com/token";
|
|
32
|
+
var EXPIRY_SKEW_MS = 60 * 1e3;
|
|
33
|
+
var cached = null;
|
|
34
|
+
function hasGoogleOAuthCredentials() {
|
|
35
|
+
return Boolean(
|
|
36
|
+
process.env.GBP_CLIENT_ID && process.env.GBP_CLIENT_SECRET && process.env.GBP_REFRESH_TOKEN
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
async function getGoogleOAuthAccessToken() {
|
|
40
|
+
if (cached && cached.expiresAt > Date.now()) {
|
|
41
|
+
return cached.accessToken;
|
|
42
|
+
}
|
|
43
|
+
if (!hasGoogleOAuthCredentials()) return null;
|
|
44
|
+
const res = await fetch(TOKEN_URI, {
|
|
45
|
+
method: "POST",
|
|
46
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
47
|
+
body: new URLSearchParams({
|
|
48
|
+
client_id: process.env.GBP_CLIENT_ID,
|
|
49
|
+
client_secret: process.env.GBP_CLIENT_SECRET,
|
|
50
|
+
refresh_token: process.env.GBP_REFRESH_TOKEN,
|
|
51
|
+
grant_type: "refresh_token"
|
|
52
|
+
})
|
|
53
|
+
});
|
|
54
|
+
if (!res.ok) {
|
|
55
|
+
throw new Error(`Google OAuth token refresh failed: ${res.status} ${await res.text()}`);
|
|
56
|
+
}
|
|
57
|
+
const data = await res.json();
|
|
58
|
+
cached = {
|
|
59
|
+
accessToken: data.access_token,
|
|
60
|
+
expiresAt: Date.now() + (data.expires_in ?? 3600) * 1e3 - EXPIRY_SKEW_MS
|
|
61
|
+
};
|
|
62
|
+
return cached.accessToken;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/reviews.ts
|
|
66
|
+
var STAR_VALUES = {
|
|
67
|
+
ONE: 1,
|
|
68
|
+
TWO: 2,
|
|
69
|
+
THREE: 3,
|
|
70
|
+
FOUR: 4,
|
|
71
|
+
FIVE: 5
|
|
72
|
+
};
|
|
73
|
+
var MAX_PAGE_SIZE = 50;
|
|
74
|
+
function shuffleArray(arr) {
|
|
75
|
+
const copy = [...arr];
|
|
76
|
+
for (let i = copy.length - 1; i > 0; i--) {
|
|
77
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
78
|
+
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
79
|
+
}
|
|
80
|
+
return copy;
|
|
81
|
+
}
|
|
82
|
+
var EMPTY = {
|
|
83
|
+
averageRating: null,
|
|
84
|
+
totalReviewCount: 0,
|
|
85
|
+
reviews: []
|
|
86
|
+
};
|
|
87
|
+
function isBusinessProfileConfigured() {
|
|
88
|
+
return hasGoogleOAuthCredentials() && Boolean(process.env.GOOGLE_BUSINESS_ACCOUNT_ID) && Boolean(process.env.GOOGLE_BUSINESS_LOCATION_ID);
|
|
89
|
+
}
|
|
90
|
+
function toBusinessReview(r) {
|
|
91
|
+
const review = {
|
|
92
|
+
id: r.reviewId,
|
|
93
|
+
author: r.reviewer?.displayName ?? "Anonymous",
|
|
94
|
+
authorPhoto: r.reviewer?.profilePhotoUrl,
|
|
95
|
+
rating: STAR_VALUES[r.starRating ?? "FIVE"] ?? 5,
|
|
96
|
+
comment: r.comment ?? "",
|
|
97
|
+
createdAt: r.createTime ?? ""
|
|
98
|
+
};
|
|
99
|
+
if (r.reviewReply?.comment) {
|
|
100
|
+
review.reply = { text: r.reviewReply.comment, updatedAt: r.reviewReply.updateTime ?? "" };
|
|
101
|
+
}
|
|
102
|
+
return review;
|
|
103
|
+
}
|
|
104
|
+
async function getBusinessReviews(options = {}) {
|
|
105
|
+
const { limit, filterMinStars, next } = options;
|
|
106
|
+
if (!isBusinessProfileConfigured()) return EMPTY;
|
|
107
|
+
const token = await getGoogleOAuthAccessToken();
|
|
108
|
+
if (!token) return EMPTY;
|
|
109
|
+
const account = process.env.GOOGLE_BUSINESS_ACCOUNT_ID;
|
|
110
|
+
const location = process.env.GOOGLE_BUSINESS_LOCATION_ID;
|
|
111
|
+
const raw = [];
|
|
112
|
+
let averageRating;
|
|
113
|
+
let totalReviewCount;
|
|
114
|
+
let pageToken;
|
|
115
|
+
do {
|
|
116
|
+
const endpoint = `https://mybusiness.googleapis.com/v4/accounts/${account}/locations/${location}/reviews?orderBy=updateTime%20desc&pageSize=${MAX_PAGE_SIZE}` + (pageToken ? `&pageToken=${encodeURIComponent(pageToken)}` : "");
|
|
117
|
+
const fetchOptions = {
|
|
118
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
119
|
+
next: next ?? { revalidate: 3600, tags: ["google-reviews"] }
|
|
120
|
+
};
|
|
121
|
+
const res = await fetch(endpoint, fetchOptions);
|
|
122
|
+
if (!res.ok) {
|
|
123
|
+
throw new Error(`Business Profile reviews failed: ${res.status} ${await res.text()}`);
|
|
124
|
+
}
|
|
125
|
+
const data = await res.json();
|
|
126
|
+
raw.push(...data.reviews ?? []);
|
|
127
|
+
averageRating = data.averageRating ?? averageRating;
|
|
128
|
+
totalReviewCount = data.totalReviewCount ?? totalReviewCount;
|
|
129
|
+
pageToken = data.nextPageToken;
|
|
130
|
+
} while (pageToken && (limit === void 0 || raw.length < limit));
|
|
131
|
+
const reviews = raw.filter((r) => r.comment && r.starRating).filter((r) => filterMinStars === void 0 || STAR_VALUES[r.starRating] >= filterMinStars).map(toBusinessReview).slice(0, limit);
|
|
132
|
+
return {
|
|
133
|
+
averageRating: averageRating ?? null,
|
|
134
|
+
totalReviewCount: totalReviewCount ?? reviews.length,
|
|
135
|
+
reviews: shuffleArray(reviews)
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
139
|
+
0 && (module.exports = {
|
|
140
|
+
getBusinessReviews,
|
|
141
|
+
getGoogleOAuthAccessToken,
|
|
142
|
+
hasGoogleOAuthCredentials,
|
|
143
|
+
isBusinessProfileConfigured
|
|
144
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google user OAuth (refresh-token) auth -- for APIs that reject service
|
|
3
|
+
* accounts, notably the Business Profile API (all scopes) and Search Console
|
|
4
|
+
* on domain-verified properties.
|
|
5
|
+
*
|
|
6
|
+
* This code is identity-agnostic: which actual Google Cloud OAuth client it
|
|
7
|
+
* talks to (the agency's shared client, or a business's own dedicated client)
|
|
8
|
+
* is entirely a function of which GBP_CLIENT_ID / GBP_CLIENT_SECRET /
|
|
9
|
+
* GBP_REFRESH_TOKEN values are set in the deploying app's environment -- not
|
|
10
|
+
* a code-level distinction. Each app/deployment configures its own.
|
|
11
|
+
*
|
|
12
|
+
* Env: GBP_CLIENT_ID, GBP_CLIENT_SECRET, GBP_REFRESH_TOKEN.
|
|
13
|
+
*/
|
|
14
|
+
/** True when the OAuth client + refresh token are present. */
|
|
15
|
+
declare function hasGoogleOAuthCredentials(): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Exchange the stored refresh token for a short-lived access token. Returns
|
|
18
|
+
* `null` when credentials aren't configured so callers can degrade
|
|
19
|
+
* gracefully. The token's actual scopes are whatever the refresh token was
|
|
20
|
+
* granted with -- not scoped per call.
|
|
21
|
+
*/
|
|
22
|
+
declare function getGoogleOAuthAccessToken(): Promise<string | null>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Google Business Profile -- fetch the business's own reviews, including the
|
|
26
|
+
* business's reply to each one. Free (no per-call billing, unlike Places
|
|
27
|
+
* API), and because these are your own reviews via the Business Profile API
|
|
28
|
+
* there are no Places display / caching restrictions.
|
|
29
|
+
*
|
|
30
|
+
* Auth is OAuth user refresh-token, not a service account -- service accounts
|
|
31
|
+
* are rejected by this API entirely. See ./oauth-client.ts.
|
|
32
|
+
*
|
|
33
|
+
* A review's reply has no author field of its own: GBP replies are 1:1 (one
|
|
34
|
+
* slot per review) and always from the business -- there's no third party to
|
|
35
|
+
* disambiguate. Google's own UI renders it as "Response from the owner" for
|
|
36
|
+
* the same reason. Attribute it to the business in the consumer, not here.
|
|
37
|
+
*
|
|
38
|
+
* Access caveat: the Business Profile APIs require a one-time access request
|
|
39
|
+
* / approval from Google, and reviews live on the legacy `mybusiness v4`
|
|
40
|
+
* endpoint which must be enabled on your Cloud project. Until that is
|
|
41
|
+
* granted, every function here degrades gracefully to empty results.
|
|
42
|
+
*
|
|
43
|
+
* Env: GBP_CLIENT_ID, GBP_CLIENT_SECRET, GBP_REFRESH_TOKEN,
|
|
44
|
+
* GOOGLE_BUSINESS_ACCOUNT_ID, GOOGLE_BUSINESS_LOCATION_ID.
|
|
45
|
+
*/
|
|
46
|
+
interface ReviewReply {
|
|
47
|
+
text: string;
|
|
48
|
+
/** ISO 8601, when the reply was last posted/edited. */
|
|
49
|
+
updatedAt: string;
|
|
50
|
+
}
|
|
51
|
+
interface BusinessReview {
|
|
52
|
+
id: string;
|
|
53
|
+
author: string;
|
|
54
|
+
authorPhoto?: string;
|
|
55
|
+
rating: number;
|
|
56
|
+
comment: string;
|
|
57
|
+
createdAt: string;
|
|
58
|
+
reply?: ReviewReply;
|
|
59
|
+
}
|
|
60
|
+
interface BusinessReviewsResult {
|
|
61
|
+
averageRating: number | null;
|
|
62
|
+
totalReviewCount: number;
|
|
63
|
+
reviews: BusinessReview[];
|
|
64
|
+
}
|
|
65
|
+
/** True when account + location + OAuth credentials are all configured. */
|
|
66
|
+
declare function isBusinessProfileConfigured(): boolean;
|
|
67
|
+
interface GetBusinessReviewsOptions {
|
|
68
|
+
/** Max reviews to return (default: all of them, paginated). */
|
|
69
|
+
limit?: number;
|
|
70
|
+
/**
|
|
71
|
+
* When set, only reviews at or above this star rating are returned -- for
|
|
72
|
+
* public-facing social proof (e.g. a marketing site's testimonial feed).
|
|
73
|
+
* Omit to return every review including low ratings, for an owner-facing
|
|
74
|
+
* surface where the point is to see and respond to everything.
|
|
75
|
+
*/
|
|
76
|
+
filterMinStars?: number;
|
|
77
|
+
/** Cache-hint passed straight through to `fetch`'s Next.js augmentation.
|
|
78
|
+
* A no-op outside Next.js. Default: revalidate hourly. */
|
|
79
|
+
next?: {
|
|
80
|
+
revalidate?: number;
|
|
81
|
+
tags?: string[];
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Fetch reviews for the configured location, paginating through all pages
|
|
86
|
+
* (GBP caps each page at 50). Returns empty results (never throws on missing
|
|
87
|
+
* config) so UI can render unconditionally.
|
|
88
|
+
*/
|
|
89
|
+
declare function getBusinessReviews(options?: GetBusinessReviewsOptions): Promise<BusinessReviewsResult>;
|
|
90
|
+
|
|
91
|
+
export { type BusinessReview, type BusinessReviewsResult, type GetBusinessReviewsOptions, type ReviewReply, getBusinessReviews, getGoogleOAuthAccessToken, hasGoogleOAuthCredentials, isBusinessProfileConfigured };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google user OAuth (refresh-token) auth -- for APIs that reject service
|
|
3
|
+
* accounts, notably the Business Profile API (all scopes) and Search Console
|
|
4
|
+
* on domain-verified properties.
|
|
5
|
+
*
|
|
6
|
+
* This code is identity-agnostic: which actual Google Cloud OAuth client it
|
|
7
|
+
* talks to (the agency's shared client, or a business's own dedicated client)
|
|
8
|
+
* is entirely a function of which GBP_CLIENT_ID / GBP_CLIENT_SECRET /
|
|
9
|
+
* GBP_REFRESH_TOKEN values are set in the deploying app's environment -- not
|
|
10
|
+
* a code-level distinction. Each app/deployment configures its own.
|
|
11
|
+
*
|
|
12
|
+
* Env: GBP_CLIENT_ID, GBP_CLIENT_SECRET, GBP_REFRESH_TOKEN.
|
|
13
|
+
*/
|
|
14
|
+
/** True when the OAuth client + refresh token are present. */
|
|
15
|
+
declare function hasGoogleOAuthCredentials(): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Exchange the stored refresh token for a short-lived access token. Returns
|
|
18
|
+
* `null` when credentials aren't configured so callers can degrade
|
|
19
|
+
* gracefully. The token's actual scopes are whatever the refresh token was
|
|
20
|
+
* granted with -- not scoped per call.
|
|
21
|
+
*/
|
|
22
|
+
declare function getGoogleOAuthAccessToken(): Promise<string | null>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Google Business Profile -- fetch the business's own reviews, including the
|
|
26
|
+
* business's reply to each one. Free (no per-call billing, unlike Places
|
|
27
|
+
* API), and because these are your own reviews via the Business Profile API
|
|
28
|
+
* there are no Places display / caching restrictions.
|
|
29
|
+
*
|
|
30
|
+
* Auth is OAuth user refresh-token, not a service account -- service accounts
|
|
31
|
+
* are rejected by this API entirely. See ./oauth-client.ts.
|
|
32
|
+
*
|
|
33
|
+
* A review's reply has no author field of its own: GBP replies are 1:1 (one
|
|
34
|
+
* slot per review) and always from the business -- there's no third party to
|
|
35
|
+
* disambiguate. Google's own UI renders it as "Response from the owner" for
|
|
36
|
+
* the same reason. Attribute it to the business in the consumer, not here.
|
|
37
|
+
*
|
|
38
|
+
* Access caveat: the Business Profile APIs require a one-time access request
|
|
39
|
+
* / approval from Google, and reviews live on the legacy `mybusiness v4`
|
|
40
|
+
* endpoint which must be enabled on your Cloud project. Until that is
|
|
41
|
+
* granted, every function here degrades gracefully to empty results.
|
|
42
|
+
*
|
|
43
|
+
* Env: GBP_CLIENT_ID, GBP_CLIENT_SECRET, GBP_REFRESH_TOKEN,
|
|
44
|
+
* GOOGLE_BUSINESS_ACCOUNT_ID, GOOGLE_BUSINESS_LOCATION_ID.
|
|
45
|
+
*/
|
|
46
|
+
interface ReviewReply {
|
|
47
|
+
text: string;
|
|
48
|
+
/** ISO 8601, when the reply was last posted/edited. */
|
|
49
|
+
updatedAt: string;
|
|
50
|
+
}
|
|
51
|
+
interface BusinessReview {
|
|
52
|
+
id: string;
|
|
53
|
+
author: string;
|
|
54
|
+
authorPhoto?: string;
|
|
55
|
+
rating: number;
|
|
56
|
+
comment: string;
|
|
57
|
+
createdAt: string;
|
|
58
|
+
reply?: ReviewReply;
|
|
59
|
+
}
|
|
60
|
+
interface BusinessReviewsResult {
|
|
61
|
+
averageRating: number | null;
|
|
62
|
+
totalReviewCount: number;
|
|
63
|
+
reviews: BusinessReview[];
|
|
64
|
+
}
|
|
65
|
+
/** True when account + location + OAuth credentials are all configured. */
|
|
66
|
+
declare function isBusinessProfileConfigured(): boolean;
|
|
67
|
+
interface GetBusinessReviewsOptions {
|
|
68
|
+
/** Max reviews to return (default: all of them, paginated). */
|
|
69
|
+
limit?: number;
|
|
70
|
+
/**
|
|
71
|
+
* When set, only reviews at or above this star rating are returned -- for
|
|
72
|
+
* public-facing social proof (e.g. a marketing site's testimonial feed).
|
|
73
|
+
* Omit to return every review including low ratings, for an owner-facing
|
|
74
|
+
* surface where the point is to see and respond to everything.
|
|
75
|
+
*/
|
|
76
|
+
filterMinStars?: number;
|
|
77
|
+
/** Cache-hint passed straight through to `fetch`'s Next.js augmentation.
|
|
78
|
+
* A no-op outside Next.js. Default: revalidate hourly. */
|
|
79
|
+
next?: {
|
|
80
|
+
revalidate?: number;
|
|
81
|
+
tags?: string[];
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Fetch reviews for the configured location, paginating through all pages
|
|
86
|
+
* (GBP caps each page at 50). Returns empty results (never throws on missing
|
|
87
|
+
* config) so UI can render unconditionally.
|
|
88
|
+
*/
|
|
89
|
+
declare function getBusinessReviews(options?: GetBusinessReviewsOptions): Promise<BusinessReviewsResult>;
|
|
90
|
+
|
|
91
|
+
export { type BusinessReview, type BusinessReviewsResult, type GetBusinessReviewsOptions, type ReviewReply, getBusinessReviews, getGoogleOAuthAccessToken, hasGoogleOAuthCredentials, isBusinessProfileConfigured };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/oauth-client.ts
|
|
2
|
+
var TOKEN_URI = "https://oauth2.googleapis.com/token";
|
|
3
|
+
var EXPIRY_SKEW_MS = 60 * 1e3;
|
|
4
|
+
var cached = null;
|
|
5
|
+
function hasGoogleOAuthCredentials() {
|
|
6
|
+
return Boolean(
|
|
7
|
+
process.env.GBP_CLIENT_ID && process.env.GBP_CLIENT_SECRET && process.env.GBP_REFRESH_TOKEN
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
async function getGoogleOAuthAccessToken() {
|
|
11
|
+
if (cached && cached.expiresAt > Date.now()) {
|
|
12
|
+
return cached.accessToken;
|
|
13
|
+
}
|
|
14
|
+
if (!hasGoogleOAuthCredentials()) return null;
|
|
15
|
+
const res = await fetch(TOKEN_URI, {
|
|
16
|
+
method: "POST",
|
|
17
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
18
|
+
body: new URLSearchParams({
|
|
19
|
+
client_id: process.env.GBP_CLIENT_ID,
|
|
20
|
+
client_secret: process.env.GBP_CLIENT_SECRET,
|
|
21
|
+
refresh_token: process.env.GBP_REFRESH_TOKEN,
|
|
22
|
+
grant_type: "refresh_token"
|
|
23
|
+
})
|
|
24
|
+
});
|
|
25
|
+
if (!res.ok) {
|
|
26
|
+
throw new Error(`Google OAuth token refresh failed: ${res.status} ${await res.text()}`);
|
|
27
|
+
}
|
|
28
|
+
const data = await res.json();
|
|
29
|
+
cached = {
|
|
30
|
+
accessToken: data.access_token,
|
|
31
|
+
expiresAt: Date.now() + (data.expires_in ?? 3600) * 1e3 - EXPIRY_SKEW_MS
|
|
32
|
+
};
|
|
33
|
+
return cached.accessToken;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/reviews.ts
|
|
37
|
+
var STAR_VALUES = {
|
|
38
|
+
ONE: 1,
|
|
39
|
+
TWO: 2,
|
|
40
|
+
THREE: 3,
|
|
41
|
+
FOUR: 4,
|
|
42
|
+
FIVE: 5
|
|
43
|
+
};
|
|
44
|
+
var MAX_PAGE_SIZE = 50;
|
|
45
|
+
function shuffleArray(arr) {
|
|
46
|
+
const copy = [...arr];
|
|
47
|
+
for (let i = copy.length - 1; i > 0; i--) {
|
|
48
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
49
|
+
[copy[i], copy[j]] = [copy[j], copy[i]];
|
|
50
|
+
}
|
|
51
|
+
return copy;
|
|
52
|
+
}
|
|
53
|
+
var EMPTY = {
|
|
54
|
+
averageRating: null,
|
|
55
|
+
totalReviewCount: 0,
|
|
56
|
+
reviews: []
|
|
57
|
+
};
|
|
58
|
+
function isBusinessProfileConfigured() {
|
|
59
|
+
return hasGoogleOAuthCredentials() && Boolean(process.env.GOOGLE_BUSINESS_ACCOUNT_ID) && Boolean(process.env.GOOGLE_BUSINESS_LOCATION_ID);
|
|
60
|
+
}
|
|
61
|
+
function toBusinessReview(r) {
|
|
62
|
+
const review = {
|
|
63
|
+
id: r.reviewId,
|
|
64
|
+
author: r.reviewer?.displayName ?? "Anonymous",
|
|
65
|
+
authorPhoto: r.reviewer?.profilePhotoUrl,
|
|
66
|
+
rating: STAR_VALUES[r.starRating ?? "FIVE"] ?? 5,
|
|
67
|
+
comment: r.comment ?? "",
|
|
68
|
+
createdAt: r.createTime ?? ""
|
|
69
|
+
};
|
|
70
|
+
if (r.reviewReply?.comment) {
|
|
71
|
+
review.reply = { text: r.reviewReply.comment, updatedAt: r.reviewReply.updateTime ?? "" };
|
|
72
|
+
}
|
|
73
|
+
return review;
|
|
74
|
+
}
|
|
75
|
+
async function getBusinessReviews(options = {}) {
|
|
76
|
+
const { limit, filterMinStars, next } = options;
|
|
77
|
+
if (!isBusinessProfileConfigured()) return EMPTY;
|
|
78
|
+
const token = await getGoogleOAuthAccessToken();
|
|
79
|
+
if (!token) return EMPTY;
|
|
80
|
+
const account = process.env.GOOGLE_BUSINESS_ACCOUNT_ID;
|
|
81
|
+
const location = process.env.GOOGLE_BUSINESS_LOCATION_ID;
|
|
82
|
+
const raw = [];
|
|
83
|
+
let averageRating;
|
|
84
|
+
let totalReviewCount;
|
|
85
|
+
let pageToken;
|
|
86
|
+
do {
|
|
87
|
+
const endpoint = `https://mybusiness.googleapis.com/v4/accounts/${account}/locations/${location}/reviews?orderBy=updateTime%20desc&pageSize=${MAX_PAGE_SIZE}` + (pageToken ? `&pageToken=${encodeURIComponent(pageToken)}` : "");
|
|
88
|
+
const fetchOptions = {
|
|
89
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
90
|
+
next: next ?? { revalidate: 3600, tags: ["google-reviews"] }
|
|
91
|
+
};
|
|
92
|
+
const res = await fetch(endpoint, fetchOptions);
|
|
93
|
+
if (!res.ok) {
|
|
94
|
+
throw new Error(`Business Profile reviews failed: ${res.status} ${await res.text()}`);
|
|
95
|
+
}
|
|
96
|
+
const data = await res.json();
|
|
97
|
+
raw.push(...data.reviews ?? []);
|
|
98
|
+
averageRating = data.averageRating ?? averageRating;
|
|
99
|
+
totalReviewCount = data.totalReviewCount ?? totalReviewCount;
|
|
100
|
+
pageToken = data.nextPageToken;
|
|
101
|
+
} while (pageToken && (limit === void 0 || raw.length < limit));
|
|
102
|
+
const reviews = raw.filter((r) => r.comment && r.starRating).filter((r) => filterMinStars === void 0 || STAR_VALUES[r.starRating] >= filterMinStars).map(toBusinessReview).slice(0, limit);
|
|
103
|
+
return {
|
|
104
|
+
averageRating: averageRating ?? null,
|
|
105
|
+
totalReviewCount: totalReviewCount ?? reviews.length,
|
|
106
|
+
reviews: shuffleArray(reviews)
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export {
|
|
110
|
+
getBusinessReviews,
|
|
111
|
+
getGoogleOAuthAccessToken,
|
|
112
|
+
hasGoogleOAuthCredentials,
|
|
113
|
+
isBusinessProfileConfigured
|
|
114
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@domandigital/gbp",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Zero-dependency Google Business Profile client: OAuth refresh-token auth and a reviews (with owner replies) fetch, shared across Doman Digital / IDS client sites.",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/index.cjs",
|
|
9
|
+
"module": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"README.md",
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"prepublishOnly": "pnpm run build"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsup": "^8.3.5",
|
|
34
|
+
"typescript": "^5.6.3",
|
|
35
|
+
"vitest": "^2.1.9"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18"
|
|
39
|
+
},
|
|
40
|
+
"packageManager": "pnpm@9.15.9",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/Doman-Digital/dd-gbp.git"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/Doman-Digital/dd-gbp#readme",
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/Doman-Digital/dd-gbp/issues"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public",
|
|
51
|
+
"provenance": true
|
|
52
|
+
}
|
|
53
|
+
}
|