Skip to content

Commit

Permalink
lint..
Browse files Browse the repository at this point in the history
  • Loading branch information
daveads committed Jan 5, 2025
1 parent e1231c4 commit 508c3a3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 27 deletions.
27 changes: 23 additions & 4 deletions source/commands/env/export/generators/ConditionSetGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import { Permit } from 'permitio';
import { HCLGenerator, WarningCollector } from '../types.js';
import { createSafeId } from '../utils.js';
import Handlebars from 'handlebars';
import Handlebars, { TemplateDelegate } from 'handlebars';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { ResourceRead } from 'permitio/build/main/openapi/types';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

interface ConditionSetData {
key: string;
name: string;
description?: string;
conditions: string;
resource?: ResourceRead;
resourceType: string;
}

type ValidConditionSet = {
key: string;
name: string;
description?: string;
conditions: string;
resource?: ResourceRead;
resourceType: string;
} | null;

export class ConditionSetGenerator implements HCLGenerator {
name = 'condition sets';
private template: HandlebarsTemplateDelegate;
private template: TemplateDelegate<{ conditionSets: ConditionSetData[] }>;

constructor(
private permit: Permit,
Expand All @@ -34,7 +53,7 @@ export class ConditionSetGenerator implements HCLGenerator {
}

const validSets = conditionSets
.map(set => {
.map<ValidConditionSet>(set => {
try {
const isResourceSet = set.type === 'resourceset';
const resourceType = isResourceSet ? 'resource_set' : 'user_set';
Expand All @@ -58,7 +77,7 @@ export class ConditionSetGenerator implements HCLGenerator {
return null;
}
})
.filter(Boolean); // Remove null values from failed conversions
.filter((set): set is ConditionSetData => set !== null);

if (validSets.length === 0) return '';

Expand Down
22 changes: 17 additions & 5 deletions source/commands/env/export/generators/RelationGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Permit } from 'permitio';
import { HCLGenerator, WarningCollector } from '../types.js';
import { createSafeId } from '../utils.js';
import Handlebars from 'handlebars';
import Handlebars, { TemplateDelegate } from 'handlebars';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
Expand All @@ -15,11 +15,21 @@ interface RelationData {
subject_resource: string;
object_resource: string;
description?: string;
[key: string]: unknown;
}

interface RawRelation {
key: string;
name: string;
subject_resource: string;
object_resource: string;
description?: string;
[key: string]: unknown;
}

export class RelationGenerator implements HCLGenerator {
name = 'relations';
private template: HandlebarsTemplateDelegate;
private template: TemplateDelegate;
private resourceKeys: Set<string> = new Set();

constructor(
Expand Down Expand Up @@ -57,7 +67,7 @@ export class RelationGenerator implements HCLGenerator {
return true;
}

private validateRelation(relation: any): relation is RelationData {
private validateRelation(relation: RawRelation): relation is RelationData {
const requiredFields = [
'key',
'name',
Expand Down Expand Up @@ -107,7 +117,7 @@ export class RelationGenerator implements HCLGenerator {
}

// Collect all relations
const allRelations = [];
const allRelations: RawRelation[] = [];
for (const resource of resources) {
if (resource.key === '__user') {
continue;
Expand All @@ -120,7 +130,9 @@ export class RelationGenerator implements HCLGenerator {
});

if (resourceRelations?.length) {
allRelations.push(...resourceRelations);
allRelations.push(
...(resourceRelations as unknown as RawRelation[]),
);
}
} catch (err) {
this.warningCollector.addWarning(
Expand Down
14 changes: 12 additions & 2 deletions source/commands/env/export/generators/ResourceGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { Permit } from 'permitio';
import { HCLGenerator, WarningCollector } from '../types.js';
import { createSafeId } from '../utils.js';
import Handlebars from 'handlebars';
import Handlebars, { TemplateDelegate } from 'handlebars';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Define a proper type for the resource object
interface ResourceData {
key: string;
name: string;
description?: string;
urn?: string;
actions: Record<string, any>;
attributes?: Record<string, any>;
}

export class ResourceGenerator implements HCLGenerator {
name = 'resources';
private template: HandlebarsTemplateDelegate;
private template: TemplateDelegate<{ resources: ResourceData[] }>;

constructor(
private permit: Permit,
Expand Down
18 changes: 13 additions & 5 deletions source/commands/env/export/generators/ResourceSetGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
// ResourceSetGenerator.ts
import { Permit } from 'permitio';
import { HCLGenerator, WarningCollector } from '../types.js';
import { createSafeId } from '../utils.js';
import Handlebars from 'handlebars';
import Handlebars, { TemplateDelegate } from 'handlebars';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Define a proper type for the resource set object
interface ResourceSetData {
key: string;
name: string;
description?: string;
conditions: string;
resource: string;
}

export class ResourceSetGenerator implements HCLGenerator {
name = 'resource set';
private template: HandlebarsTemplateDelegate;
private template: TemplateDelegate<{ sets: ResourceSetData[] }>;

constructor(
private permit: Permit,
Expand All @@ -28,7 +36,7 @@ export class ResourceSetGenerator implements HCLGenerator {
// Get all resource sets using the Permit SDK
const resourceSets = await this.permit.api.conditionSets.list({});

// Filter only resource sets (not user sets)
// Filter only resource sets (not user sets) and ensure `resource_id` is defined
const validSets = resourceSets
.filter(set => set.type === 'resourceset')
.map(set => ({
Expand All @@ -39,7 +47,7 @@ export class ResourceSetGenerator implements HCLGenerator {
typeof set.conditions === 'string'
? set.conditions
: JSON.stringify(set.conditions),
resource: set.resource_id,
resource: set.resource_id?.toString() || '',
}));

if (validSets.length === 0) return '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Permit } from 'permitio';
import { HCLGenerator, WarningCollector } from '../types.js';
import { createSafeId } from '../utils.js';
import Handlebars from 'handlebars';
import Handlebars, { TemplateDelegate } from 'handlebars';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
Expand All @@ -22,7 +22,7 @@ interface RoleDerivationData {

export class RoleDerivationGenerator implements HCLGenerator {
name = 'role derivation';
private template: HandlebarsTemplateDelegate;
private template: TemplateDelegate<{ derivations: RoleDerivationData[] }>;

constructor(
private permit: Permit,
Expand Down
13 changes: 11 additions & 2 deletions source/commands/env/export/generators/RoleGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { Permit } from 'permitio';
import { HCLGenerator, WarningCollector } from '../types.js';
import { createSafeId } from '../utils.js';
import Handlebars from 'handlebars';
import Handlebars, { TemplateDelegate } from 'handlebars';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Define a proper type for the role object
interface RoleData {
key: string;
name: string;
permissions: string[];
dependencies: string[];
}

// Register Handlebars helper
Handlebars.registerHelper('json', function (context: string[]) {
return `[${context.map((item: string) => `"${item}"`).join(',')}]`;
});

export class RoleGenerator implements HCLGenerator {
name = 'roles';
private template: HandlebarsTemplateDelegate;
private template: TemplateDelegate<{ roles: RoleData[] }>;

constructor(
private permit: Permit,
Expand Down
11 changes: 9 additions & 2 deletions source/commands/env/export/generators/UserAttributesGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { Permit } from 'permitio';
import { HCLGenerator, WarningCollector } from '../types.js';
import { createSafeId } from '../utils.js';
import Handlebars from 'handlebars';
import Handlebars, { TemplateDelegate } from 'handlebars';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Define a proper type for the user attribute object
interface UserAttributeData {
key: string;
type: string;
description?: string;
}

export class UserAttributesGenerator implements HCLGenerator {
name = 'user attributes';
private template: HandlebarsTemplateDelegate;
private template: TemplateDelegate<{ attributes: UserAttributeData[] }>;

constructor(
private permit: Permit,
Expand Down
19 changes: 14 additions & 5 deletions source/commands/env/export/generators/UserSetGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { Permit } from 'permitio';
import { HCLGenerator, WarningCollector } from '../types.js';
import { createSafeId } from '../utils.js';
import Handlebars from 'handlebars';
import Handlebars, { TemplateDelegate } from 'handlebars';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Define a proper type for the user set object
interface UserSetData {
key: string;
name: string;
description?: string;
conditions: string;
resource: string; // Ensure `resource` is always a string
}

export class UserSetGenerator implements HCLGenerator {
name = 'user set';
private template: HandlebarsTemplateDelegate;
private template: TemplateDelegate<{ sets: UserSetData[] }>;

constructor(
private permit: Permit,
Expand All @@ -27,9 +36,9 @@ export class UserSetGenerator implements HCLGenerator {
// Get all condition sets using the Permit SDK
const conditionSets = await this.permit.api.conditionSets.list({});

// Filter only user sets (not resource sets)
// Filter only user sets (not resource sets) and ensure `resource_id` is defined
const validSets = conditionSets
.filter(set => set.type === 'userset')
.filter(set => set.type === 'userset' && set.resource_id !== undefined) // Ensure `resource_id` is defined
.map(set => ({
key: createSafeId(set.key),
name: set.name,
Expand All @@ -38,7 +47,7 @@ export class UserSetGenerator implements HCLGenerator {
typeof set.conditions === 'string'
? set.conditions
: JSON.stringify(set.conditions),
resource: set.resource_id,
resource: set.resource_id, // `resource_id` is now guaranteed to be defined
}));

if (validSets.length === 0) return '';
Expand Down

0 comments on commit 508c3a3

Please sign in to comment.