Edits history of script submission #9862 for ' Create Schedule (enode)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Enode = {
    	token: string
    }
    
    export async function main(
    	auth: Enode,
    	userId: string,
    	body:
    		| ({
    				isEnabled?: false | true
    				defaultShouldCharge?: false | true
    				rules?: {
    					hourMinute?: { from: string; to: string }
    					fromTimestamp?: string
    					toTimestamp?: string
    					weekdays?: 0 | 1 | 2 | 3 | 4 | 5 | 6[]
    				} & { shouldCharge: false | true }[]
    				targetId?: string
    				targetType?: 'vehicle' | 'charger'
    				locationId?: string
    		  } & {})
    		| ({
    				isEnabled?: false | true
    				targetId?: string
    				targetType?: 'hvac'
    				defaultTargetState?:
    					| ({ coolSetpoint: number; mode: 'COOL'; holdType: 'PERMANENT' } & {})
    					| ({ heatSetpoint: number; mode: 'HEAT'; holdType: 'PERMANENT' } & {})
    					| ({
    							coolSetpoint: number
    							heatSetpoint: number
    							mode: 'AUTO'
    							holdType: 'PERMANENT'
    					  } & {})
    					| ({ mode: 'OFF'; holdType: 'PERMANENT' } & {})
    					| ({ holdType: 'SCHEDULED' } & {})
    				rules?: {
    					hourMinute?: { from: string; to: string }
    					fromTimestamp?: string
    					toTimestamp?: string
    					weekdays?: 0 | 1 | 2 | 3 | 4 | 5 | 6[]
    				} & {
    					targetState:
    						| ({
    								coolSetpoint: number
    								mode: 'COOL'
    								holdType: 'PERMANENT'
    						  } & {})
    						| ({
    								heatSetpoint: number
    								mode: 'HEAT'
    								holdType: 'PERMANENT'
    						  } & {})
    						| ({
    								coolSetpoint: number
    								heatSetpoint: number
    								mode: 'AUTO'
    								holdType: 'PERMANENT'
    						  } & {})
    						| ({ mode: 'OFF'; holdType: 'PERMANENT' } & {})
    						| ({ holdType: 'SCHEDULED' } & {})
    				}[]
    		  } & {})
    ) {
    	const url = new URL(`https://enode-api.production.enode.io/users/${userId}/schedules`)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + auth.token
    		},
    		body: JSON.stringify(body)
    	})
    
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    
    	return await response.json()
    }
    

    Submitted by hugo697 581 days ago