feat(coding): add Pi Data Service product tools
This commit is contained in:
@@ -77,16 +77,16 @@ type FetchImplementation = typeof fetch;
|
||||
type AccessTokenGetter = typeof getValidWorksSquareAccessToken;
|
||||
|
||||
export type DataServiceOperations = {
|
||||
configure(input: { collections: string[] }): Promise<DataServiceHostResult<DataServiceInstanceState>>;
|
||||
inspect(): Promise<DataServiceHostResult<DataServiceInstanceState>>;
|
||||
configure(input: { collections: string[] }, trustedProjectPath?: string): Promise<DataServiceHostResult<DataServiceInstanceState>>;
|
||||
inspect(trustedProjectPath?: string): Promise<DataServiceHostResult<DataServiceInstanceState>>;
|
||||
listProjects(): Promise<DataServiceHostResult<DataServiceInstanceList>>;
|
||||
getDocument(input: { collection: string; document_id: string }): Promise<DataServiceHostResult<DataServiceDocument>>;
|
||||
listDocuments(input: { collection: string; limit?: number; cursor?: string }): Promise<DataServiceHostResult<DataServiceDocumentList>>;
|
||||
putDocument(input: DataServicePutDocumentInput): Promise<DataServiceHostResult<DataServiceDocument>>;
|
||||
deleteDocument(input: DataServiceDocumentTargetInput & { confirmed: true }): Promise<DataServiceHostResult<null>>;
|
||||
removeCollection(input: DataServiceCollectionTargetInput): Promise<DataServiceHostResult<DataServiceCollectionRemoval>>;
|
||||
reset(input: DataServiceConfirmationInput): Promise<DataServiceHostResult<DataServiceInstanceState>>;
|
||||
removeProject(input: DataServiceConfirmationInput): Promise<DataServiceHostResult<DataServiceInstanceRemoval>>;
|
||||
getDocument(input: { collection: string; document_id: string }, trustedProjectPath?: string): Promise<DataServiceHostResult<DataServiceDocument>>;
|
||||
listDocuments(input: { collection: string; limit?: number; cursor?: string }, trustedProjectPath?: string): Promise<DataServiceHostResult<DataServiceDocumentList>>;
|
||||
putDocument(input: DataServicePutDocumentInput, trustedProjectPath?: string): Promise<DataServiceHostResult<DataServiceDocument>>;
|
||||
deleteDocument(input: DataServiceDocumentTargetInput & { confirmed: true }, trustedProjectPath?: string): Promise<DataServiceHostResult<null>>;
|
||||
removeCollection(input: DataServiceCollectionTargetInput, trustedProjectPath?: string): Promise<DataServiceHostResult<DataServiceCollectionRemoval>>;
|
||||
reset(input: DataServiceConfirmationInput, trustedProjectPath?: string): Promise<DataServiceHostResult<DataServiceInstanceState>>;
|
||||
removeProject(input: DataServiceConfirmationInput, trustedProjectPath?: string): Promise<DataServiceHostResult<DataServiceInstanceRemoval>>;
|
||||
};
|
||||
|
||||
export type DataServiceCloudClientDependencies = {
|
||||
@@ -747,10 +747,11 @@ export function createDataServiceOperations(
|
||||
const client = dependencies.client ?? new DataServiceCloudClient();
|
||||
|
||||
async function withActive<T>(
|
||||
trustedProjectPath: string | undefined,
|
||||
operation: (projectId: string) => Promise<DataServiceHostResult<T>>,
|
||||
): Promise<DataServiceHostResult<T>> {
|
||||
try {
|
||||
const active = await dependencies.projects.requireActiveRealProjectWithIdentity();
|
||||
const active = await dependencies.projects.requireActiveRealProjectWithIdentity(trustedProjectPath);
|
||||
return await operation(active.projectId);
|
||||
} catch (error) {
|
||||
return projectLocalProjectError<T>(error);
|
||||
@@ -759,26 +760,26 @@ export function createDataServiceOperations(
|
||||
|
||||
return {
|
||||
listProjects: () => client.listProjects(),
|
||||
configure: ({ collections }) => withActive((projectId) => client.configure(projectId, collections)),
|
||||
inspect: () => withActive((projectId) => client.inspect(projectId)),
|
||||
getDocument: ({ collection, document_id }) => withActive(
|
||||
configure: ({ collections }, trustedProjectPath) => withActive(trustedProjectPath, (projectId) => client.configure(projectId, collections)),
|
||||
inspect: (trustedProjectPath) => withActive(trustedProjectPath, (projectId) => client.inspect(projectId)),
|
||||
getDocument: ({ collection, document_id }, trustedProjectPath) => withActive(trustedProjectPath,
|
||||
(projectId) => client.getDocument(projectId, collection, document_id),
|
||||
),
|
||||
listDocuments: ({ collection, limit, cursor }) => withActive(
|
||||
listDocuments: ({ collection, limit, cursor }, trustedProjectPath) => withActive(trustedProjectPath,
|
||||
(projectId) => client.listDocuments(projectId, collection, limit, cursor),
|
||||
),
|
||||
putDocument: (input) => withActive((projectId) => client.putDocument(projectId, input)),
|
||||
deleteDocument: ({ collection, document_id, if_revision, confirmed }) => confirmed === true
|
||||
? withActive((projectId) => client.deleteDocument(projectId, { collection, document_id, if_revision }))
|
||||
putDocument: (input, trustedProjectPath) => withActive(trustedProjectPath, (projectId) => client.putDocument(projectId, input)),
|
||||
deleteDocument: ({ collection, document_id, if_revision, confirmed }, trustedProjectPath) => confirmed === true
|
||||
? withActive(trustedProjectPath, (projectId) => client.deleteDocument(projectId, { collection, document_id, if_revision }))
|
||||
: Promise.resolve(confirmationRequired()),
|
||||
removeCollection: ({ collection, confirmed }) => confirmed === true
|
||||
? withActive((projectId) => client.removeCollection(projectId, collection))
|
||||
removeCollection: ({ collection, confirmed }, trustedProjectPath) => confirmed === true
|
||||
? withActive(trustedProjectPath, (projectId) => client.removeCollection(projectId, collection))
|
||||
: Promise.resolve(confirmationRequired()),
|
||||
reset: ({ confirmed }) => confirmed === true
|
||||
? withActive((projectId) => client.reset(projectId))
|
||||
reset: ({ confirmed }, trustedProjectPath) => confirmed === true
|
||||
? withActive(trustedProjectPath, (projectId) => client.reset(projectId))
|
||||
: Promise.resolve(confirmationRequired()),
|
||||
removeProject: ({ confirmed }) => confirmed === true
|
||||
? withActive((projectId) => client.removeProject(projectId))
|
||||
removeProject: ({ confirmed }, trustedProjectPath) => confirmed === true
|
||||
? withActive(trustedProjectPath, (projectId) => client.removeProject(projectId))
|
||||
: Promise.resolve(confirmationRequired()),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user