Saltearse al contenido

virtuals/sdk/modules/rest_api

Esta página aún no está disponible en tu idioma.

Defined in: studiocms/packages/studiocms/src/virtuals/sdk/modules/rest_api.ts:33^

  • any

new SDKCore_REST_API(): SDKCore_REST_API

SDKCore_REST_API

Effect.Service<SDKCore_REST_API>()(
'studiocms/sdk/SDKCore/modules/rest_api',
{
dependencies: [AstroDB.Default, SDKCore_Generators.Default],
effect: genLogger('studiocms/sdk/SDKCore/modules/rest_api/effect')(function () {
const [dbService, { generateToken }] = yield Effect.all([AstroDB, SDKCore_Generators]);
const REST_API = {
tokens: {
/
Retrieves all API tokens for a specific user.
@param userId - The ID of the user whose tokens are to be retrieved.
@returns An Effect that resolves to an array of API keys for the user.
@throws {LibSQLDatabaseError} If a database error occurs during the operation.
/
get: dbService.makeQuery((ex, userId: string) =>
ex((db) => db.select().from(tsAPIKeys).where(eq(tsAPIKeys.userId, userId))).pipe(
Effect.catchTags({
'studiocms/sdk/effect/db/LibSQLDatabaseError': (cause) =>
_clearLibSQLError('REST_API.tokens.get', cause),
})
)
),
/
Creates a new API token for a user with the specified description.
@param userId - The ID of the user for whom to create the token.
@param description - A description for the API key.
@returns An Effect that resolves to the created API key record.
@throws {LibSQLDatabaseError} If a database error occurs during the operation.
/
new: (userId: string, description: string) =>
Effect.gen(function () {
const key = yield generateToken(userId, true);
return yield dbService.execute((db) =>
db
.insert(tsAPIKeys)
.values({
id: crypto.randomUUID(),
creationDate: new Date(),
userId,
key,
description,
})
.returning()
.get()
);
}).pipe(
Effect.catchTags({
'studiocms/sdk/effect/db/LibSQLDatabaseError': (cause) =>
_clearLibSQLError('REST_API.tokens.new', cause),
})
),
/
Deletes an API token for a user by its ID.
@param userId - The ID of the user whose token is to be deleted.
@param tokenId - The ID of the API token to delete.
@returns An Effect that resolves when the token is successfully deleted.
@throws {LibSQLDatabaseError} If a database error occurs during the operation.
/
delete: (userId: string, tokenId: string) =>
dbService
.execute((db) =>
db
.delete(tsAPIKeys)
.where(and(eq(tsAPIKeys.userId, userId), eq(tsAPIKeys.id, tokenId)))
)
.pipe(
Effect.catchTags({
'studiocms/sdk/effect/db/LibSQLDatabaseError': (cause) =>
_clearLibSQLError('REST_API.tokens.delete', cause),
})
),
/
Verifies an API key and retrieves the associated user ID and rank.
@param key - The API key to verify.
@returns An Effect that resolves to an object containing userId, key, and rank if valid, or false if invalid.
@throws {LibSQLDatabaseError} If a database error occurs during the verification.
/
verify: (key: string) =>
Effect.gen(function () {
const apiKey = yield dbService.execute((db) =>
db.select().from(tsAPIKeys).where(eq(tsAPIKeys.key, key)).get()
);
if (!apiKey) return false;
const keyRank = yield dbService.execute((db) =>
db.select().from(tsPermissions).where(eq(tsPermissions.user, apiKey.userId)).get()
);
if (!keyRank) return false;
return {
userId: apiKey.userId,
key: apiKey.key,
rank: keyRank.rank,
};
}).pipe(
Effect.catchTags({
'studiocms/sdk/effect/db/LibSQLDatabaseError': (cause) =>
_clearLibSQLError('REST_API.tokens.verify', cause),
})
),
},
};
return REST_API;
}),
}
).constructor