Skip to content

Commit

Permalink
[pocketbase#101] add query params for all REST methods
Browse files Browse the repository at this point in the history
- make interfaces small and composable
- fix typos in log messages and comments
  • Loading branch information
sewera committed Dec 7, 2022
1 parent ac38543 commit 0af79c3
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 56 deletions.
7 changes: 6 additions & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import CollectionService from '@/services/CollectionService';
import LogService from '@/services/LogService';
import RealtimeService from '@/services/RealtimeService';
import Record from '@/models/Record';
import { BaseQueryParams } from './services/utils/BaseService';

/**
* PocketBase JS Client.
Expand Down Expand Up @@ -273,7 +274,7 @@ export default class Client {
/**
* Builds and returns an absolute record file url for the provided filename.
*/
getFileUrl(record: Record, filename: string, queryParams = {}): string {
getFileUrl(record: Record, filename: string, queryParams: FileQueryParams = {}): string {
const parts = [];
parts.push("api")
parts.push("files")
Expand Down Expand Up @@ -333,3 +334,7 @@ export default class Client {
return result.join('&');
}
}

export interface FileQueryParams extends BaseQueryParams {
thumb?: string;
}
2 changes: 1 addition & 1 deletion src/models/LogRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class LogRequest extends BaseModel {
load(data: { [key: string]: any }) {
super.load(data);

// fallback to the ip field for backward compatability
// fallback to the ip field for backward compatibility
data.remoteIp = data.remoteIp || data.ip;

this.url = typeof data.url === 'string' ? data.url : '';
Expand Down
17 changes: 9 additions & 8 deletions src/services/AdminService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CrudService from '@/services/utils/CrudService';
import Admin from '@/models/Admin';
import CrudService from '@/services/utils/CrudService';
import Admin from '@/models/Admin';
import { BaseQueryParams } from './utils/BaseService';

export type AdminAuthResponse = {
[key: string]: any,
Expand Down Expand Up @@ -32,7 +33,7 @@ export default class AdminService extends CrudService<Admin> {
* If the current `client.authStore.model` matches with the updated id, then
* on success the `client.authStore.model` will be updated with the result.
*/
update<T = Admin>(id: string, bodyParams = {}, queryParams = {}): Promise<T> {
update<T = Admin>(id: string, bodyParams = {}, queryParams: BaseQueryParams = {}): Promise<T> {
return super.update<Admin>(id, bodyParams, queryParams).then((item) => {
// update the store state if the updated item id matches with the stored model
if (
Expand All @@ -53,7 +54,7 @@ export default class AdminService extends CrudService<Admin> {
* If the current `client.authStore.model` matches with the deleted id,
* then on success the `client.authStore` will be cleared.
*/
delete(id: string, queryParams = {}): Promise<boolean> {
delete(id: string, queryParams: BaseQueryParams = {}): Promise<boolean> {
return super.delete(id, queryParams).then((success) => {
// clear the store state if the deleted item id matches with the stored model
if (
Expand Down Expand Up @@ -99,7 +100,7 @@ export default class AdminService extends CrudService<Admin> {
email: string,
password: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<AdminAuthResponse> {
bodyParams = Object.assign({
'identity': email,
Expand All @@ -122,7 +123,7 @@ export default class AdminService extends CrudService<Admin> {
*
* On success this method automatically updates the client's AuthStore data.
*/
authRefresh(bodyParams = {}, queryParams = {}): Promise<AdminAuthResponse> {
authRefresh(bodyParams = {}, queryParams: BaseQueryParams = {}): Promise<AdminAuthResponse> {
return this.client.send(this.baseCrudPath + '/auth-refresh', {
'method': 'POST',
'params': queryParams,
Expand All @@ -136,7 +137,7 @@ export default class AdminService extends CrudService<Admin> {
requestPasswordReset(
email: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<boolean> {
bodyParams = Object.assign({
'email': email,
Expand All @@ -157,7 +158,7 @@ export default class AdminService extends CrudService<Admin> {
password: string,
passwordConfirm: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<boolean> {
bodyParams = Object.assign({
'token': passwordResetToken,
Expand Down
7 changes: 4 additions & 3 deletions src/services/CollectionService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CrudService from '@/services/utils/CrudService';
import Collection from '@/models/Collection';
import CrudService from '@/services/utils/CrudService';
import Collection from '@/models/Collection';
import { BaseQueryParams } from './utils/BaseService';

export default class CollectionService extends CrudService<Collection> {
/**
Expand All @@ -23,7 +24,7 @@ export default class CollectionService extends CrudService<Collection> {
* that are not present in the imported configuration, WILL BE DELETED
* (including their related records data)!
*/
async import(collections: Array<Collection>, deleteMissing: boolean = false, queryParams = {}): Promise<true> {
async import(collections: Array<Collection>, deleteMissing: boolean = false, queryParams: BaseQueryParams = {}): Promise<true> {
return this.client.send(this.baseCrudPath + '/import', {
'method': 'PUT',
'params': queryParams,
Expand Down
23 changes: 19 additions & 4 deletions src/services/LogService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import LogRequest from '@/models/LogRequest';
import ListResult from '@/models/utils/ListResult';
import BaseService from '@/services/utils/BaseService';
import BaseService, {
BaseQueryParams,
FilterableQueryParams,
PaginatedQueryParams,
SortableQueryParams
} from '@/services/utils/BaseService';

export type HourlyStats = {
total: number,
Expand All @@ -11,7 +16,7 @@ export default class LogService extends BaseService {
/**
* Returns paginated logged requests list.
*/
getRequestsList(page = 1, perPage = 30, queryParams = {}): Promise<ListResult<LogRequest>> {
getRequestsList(page = 1, perPage = 30, queryParams: ListLogsQueryParams = {}): Promise<ListResult<LogRequest>> {
queryParams = Object.assign({
'page': page,
'perPage': perPage,
Expand Down Expand Up @@ -42,7 +47,7 @@ export default class LogService extends BaseService {
/**
* Returns a single logged request by its id.
*/
getRequest(id: string, queryParams = {}): Promise<LogRequest> {
getRequest(id: string, queryParams: BaseQueryParams = {}): Promise<LogRequest> {
return this.client.send('/api/logs/requests/' + encodeURIComponent(id), {
'method': 'GET',
'params': queryParams
Expand All @@ -52,10 +57,20 @@ export default class LogService extends BaseService {
/**
* Returns request logs statistics.
*/
getRequestsStats(queryParams = {}): Promise<Array<HourlyStats>> {
getRequestsStats(queryParams: LogStatsQueryParams = {}): Promise<Array<HourlyStats>> {
return this.client.send('/api/logs/requests/stats', {
'method': 'GET',
'params': queryParams
}).then((responseData: any) => responseData);
}
}

export interface ListLogsQueryParams extends
BaseQueryParams,
PaginatedQueryParams,
SortableQueryParams,
FilterableQueryParams {}

export interface LogStatsQueryParams extends
BaseQueryParams,
FilterableQueryParams {}
33 changes: 17 additions & 16 deletions src/services/RecordService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import CrudService from '@/services/utils/CrudService';
import { UnsubscribeFunc } from '@/services/RealtimeService';
import Record from '@/models/Record';
import ExternalAuth from '@/models/ExternalAuth';
import { BaseQueryParams } from './utils/BaseService';

export interface RecordAuthResponse<T = Record> {
token: string;
Expand Down Expand Up @@ -70,7 +71,7 @@ export default class RecordService extends CrudService<Record> {
* Subscribe to the realtime changes of a single record in the collection.
*/
async subscribeOne<T = Record>(recordId: string, callback: (data: RecordSubscription<T>) => void): Promise<UnsubscribeFunc> {
console.warn("PocketBase: subscribeOne(recordId, callback) is deprecated. Please replace it with subsribe(recordId, callback).");
console.warn("PocketBase: subscribeOne(recordId, callback) is deprecated. Please replace it with subscribe(recordId, callback).");
return this.client.realtime.subscribe(this.collectionIdOrName + "/" + recordId, callback);
}

Expand Down Expand Up @@ -99,7 +100,7 @@ export default class RecordService extends CrudService<Record> {
callback?: (data: RecordSubscription<T>) => void
): Promise<UnsubscribeFunc> {
if (typeof topicOrCallback === 'function') {
console.warn("PocketBase: subscribe(callback) is deprecated. Please replace it with subsribe('*', callback).");
console.warn("PocketBase: subscribe(callback) is deprecated. Please replace it with subscribe('*', callback).");
return this.client.realtime.subscribe(this.collectionIdOrName, topicOrCallback);
}

Expand Down Expand Up @@ -150,7 +151,7 @@ export default class RecordService extends CrudService<Record> {
* If the current `client.authStore.model` matches with the updated id, then
* on success the `client.authStore.model` will be updated with the result.
*/
update<T = Record>(id: string, bodyParams = {}, queryParams = {}): Promise<T> {
update<T = Record>(id: string, bodyParams = {}, queryParams: BaseQueryParams = {}): Promise<T> {
return super.update<Record>(id, bodyParams, queryParams).then((item) => {
if (
typeof this.client.authStore.model?.collectionId !== 'undefined' && // is record auth
Expand All @@ -169,7 +170,7 @@ export default class RecordService extends CrudService<Record> {
* If the current `client.authStore.model` matches with the deleted id,
* then on success the `client.authStore` will be cleared.
*/
delete(id: string, queryParams = {}): Promise<boolean> {
delete(id: string, queryParams: BaseQueryParams = {}): Promise<boolean> {
return super.delete(id, queryParams).then((success) => {
if (
success &&
Expand Down Expand Up @@ -205,7 +206,7 @@ export default class RecordService extends CrudService<Record> {
/**
* Returns all available collection auth methods.
*/
listAuthMethods(queryParams = {}): Promise<AuthMethodsList> {
listAuthMethods(queryParams: BaseQueryParams = {}): Promise<AuthMethodsList> {
return this.client.send(this.baseCollectionPath + '/auth-methods', {
'method': 'GET',
'params': queryParams,
Expand All @@ -231,7 +232,7 @@ export default class RecordService extends CrudService<Record> {
usernameOrEmail: string,
password: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<RecordAuthResponse<T>> {
bodyParams = Object.assign({
'identity': usernameOrEmail,
Expand Down Expand Up @@ -264,7 +265,7 @@ export default class RecordService extends CrudService<Record> {
redirectUrl: string,
createData = {},
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<RecordAuthResponse<T>> {
bodyParams = Object.assign({
'provider': provider,
Expand All @@ -287,7 +288,7 @@ export default class RecordService extends CrudService<Record> {
*
* On success this method also automatically updates the client's AuthStore.
*/
authRefresh<T = Record>(bodyParams = {}, queryParams = {}): Promise<RecordAuthResponse<T>> {
authRefresh<T = Record>(bodyParams = {}, queryParams: BaseQueryParams = {}): Promise<RecordAuthResponse<T>> {
return this.client.send(this.baseCollectionPath + '/auth-refresh', {
'method': 'POST',
'params': queryParams,
Expand All @@ -301,7 +302,7 @@ export default class RecordService extends CrudService<Record> {
requestPasswordReset(
email: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<boolean> {
bodyParams = Object.assign({
'email': email,
Expand All @@ -322,7 +323,7 @@ export default class RecordService extends CrudService<Record> {
password: string,
passwordConfirm: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<boolean> {
bodyParams = Object.assign({
'token': passwordResetToken,
Expand All @@ -343,7 +344,7 @@ export default class RecordService extends CrudService<Record> {
requestVerification(
email: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<boolean> {
bodyParams = Object.assign({
'email': email,
Expand All @@ -362,7 +363,7 @@ export default class RecordService extends CrudService<Record> {
confirmVerification(
verificationToken: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<boolean> {
bodyParams = Object.assign({
'token': verificationToken,
Expand All @@ -381,7 +382,7 @@ export default class RecordService extends CrudService<Record> {
requestEmailChange(
newEmail: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<boolean> {
bodyParams = Object.assign({
'newEmail': newEmail,
Expand All @@ -401,7 +402,7 @@ export default class RecordService extends CrudService<Record> {
emailChangeToken: string,
password: string,
bodyParams = {},
queryParams = {},
queryParams: BaseQueryParams = {},
): Promise<boolean> {
bodyParams = Object.assign({
'token': emailChangeToken,
Expand All @@ -420,7 +421,7 @@ export default class RecordService extends CrudService<Record> {
*/
listExternalAuths(
recordId: string,
queryParams = {}
queryParams: BaseQueryParams = {}
): Promise<Array<ExternalAuth>> {
return this.client.send(this.baseCrudPath + '/' + encodeURIComponent(recordId) + '/external-auths', {
'method': 'GET',
Expand All @@ -444,7 +445,7 @@ export default class RecordService extends CrudService<Record> {
unlinkExternalAuth(
recordId: string,
provider: string,
queryParams = {}
queryParams: BaseQueryParams = {}
): Promise<boolean> {
return this.client.send(this.baseCrudPath + '/' + encodeURIComponent(recordId) + '/external-auths/' + encodeURIComponent(provider), {
'method': 'DELETE',
Expand Down
10 changes: 5 additions & 5 deletions src/services/SettingsService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import BaseService from '@/services/utils/BaseService';
import BaseService, { BaseQueryParams } from '@/services/utils/BaseService';

export default class SettingsService extends BaseService {
/**
* Fetch all available app settings.
*/
getAll(queryParams = {}): Promise<{ [key: string]: any }> {
getAll(queryParams: BaseQueryParams = {}): Promise<{ [key: string]: any }> {
return this.client.send('/api/settings', {
'method': 'GET',
'params': queryParams,
Expand All @@ -14,7 +14,7 @@ export default class SettingsService extends BaseService {
/**
* Bulk updates app settings.
*/
update(bodyParams = {}, queryParams = {}): Promise<{ [key: string]: any }> {
update(bodyParams = {}, queryParams: BaseQueryParams = {}): Promise<{ [key: string]: any }> {
return this.client.send('/api/settings', {
'method': 'PATCH',
'params': queryParams,
Expand All @@ -25,7 +25,7 @@ export default class SettingsService extends BaseService {
/**
* Performs a S3 storage connection test.
*/
testS3(queryParams = {}): Promise<boolean> {
testS3(queryParams: BaseQueryParams = {}): Promise<boolean> {
return this.client.send('/api/settings/test/s3', {
'method': 'POST',
'params': queryParams,
Expand All @@ -40,7 +40,7 @@ export default class SettingsService extends BaseService {
* - password-reset
* - email-change
*/
testEmail(toEmail: string, emailTemplate: string, queryParams = {}): Promise<boolean> {
testEmail(toEmail: string, emailTemplate: string, queryParams: BaseQueryParams = {}): Promise<boolean> {
const bodyParams = {
'email': toEmail,
'template': emailTemplate,
Expand Down
Loading

0 comments on commit 0af79c3

Please sign in to comment.